Imagine I have the following array of objects:
const object = [
{Name: "A", Nr: "01"},
{Name: "B", Nr: "02"},
{Name: "C", Nr: "04"},
{Name: "D", Nr: "06"},
{Name: "E", Nr: "07"},
];
And I have the following function:
const findCurrentNumber = (obj) => {
let numbers = obj.map((a) => {
return parseInt(a["Nr"]);
});
numbers.sort((a, b) => a - b);
let current = numbers[numbers.length - 1];
for (let i = numbers.length - 1; i > 0; i--) {
if (numbers[i - 1] !== numbers[i] - 1) current = numbers[i - 1];
}
return current + 1;
};
I will get the value 3 as return. But I want to do the same just using array methods (map, sort, reduce etc). I was trying and I stopped here:
const obj = object.map(a => parseInt(a["Nr"])).sort((a, b) => a - b);
I don't know how to continue from the part I declared the current variable.
Is it possible? If so, how can I do it?
Thank you!