I have a following function that iterates over array and dose some work on the item related to the array that I want to fill like this:
function fileNaming(names) {
const result = [];
names.forEach(name => {
let count = 0;
const curr = name;
if (result.includes(name)) {
while (result.includes(name)) {
count++;
name = `${curr}(${count})`;
}
}
result.push(name);
})
return result;
}
I want to get rid of the result array and use only map for it like this:
function fileNaming(names) {
return names.map(name => {
let count = 0;
const curr = name;
// In the following 2 lines we will get error because we don't have result array anymore
if (result.includes(name)) {
while (result.includes(name)) {
count++;
name = `${curr}(${count})`;
}
}
return name;
})
}
But the problem is that I need to check something inside the outputed array but I don't know how to do it. I tried to find similar problems like this but didn't found anything, also I tried to dig up in the pollifils of map method, but everywhere was just a loop with some result array that I could get access.
reducemethod may be a better option for such a problem because then you'll have theaccumulatorthat you can use for the required comparisons. (If I am not missing anything)