Is it legit (or good practice) do a loop with a higher order function - like Array.map()
- to perform some side effects ?
This is mostly a theoretical question since I notice that sometimes I found (or perform myself) some loops using the .map()
method, like this:
let myObject = {...}
let myItemsArray = [ ... , ... ]
myItemsArray.map( (item, index) => {
// do some side effect for ex:
myObject.item[index] = item
}
But I know that this map()
method actually returns an array. So calling myItemsArray.map()
is like I'm returning an array without assign it to any variable.
So the question, is this legit? Should I avoid this and use a classic for()
loop or a .forEach()
method?
Side question: one of the reasons I'm asking this is because I need to perform some loops on an async function so promises and await operators are involved, I remember that forEach()
loops are not ideal on an async function, but wanted to know why.