I'm not sure I see the value here, provided it's just syntactical difference (it's not, see the other comments). If it were just a matter of syntactical sugar, this whole post could have been addressed by bringing the Array.forEach() to Author's attention, which in the simpler terms, is an "Array.map()"-like syntax for for-loop.
i.e.
newArray=[];oldArray.forEach((element,index,thisArray)=>{// Access the index of the current elementconsole.log("Handling element at",index,"index of old array");// Work on the current element of the arraynewArray.push(transformElement(element));// Access the elements of the current array at specific index:thisArray[index]={...element,transformed:true}}
The main benefit of taking thisArray as the last argument to lambda is the fact that you can encapsulate the behaviour in a named function (here a lambda is provided). Rest is pretty obvious.
In the task I mentioned above for reversing strings and putting them in a new array, I agree that forEach is a good approach.
I personally would still prefer map() over forEach() because
we don't have to push element to newArray manually
since map() returns an array. we can do further processing on array like filtering and sorting right there after running map() without ever need to manipulate newArray.
I'm not sure I see the value here, provided it's just syntactical difference (it's not, see the other comments). If it were just a matter of syntactical sugar, this whole post could have been addressed by bringing the Array.forEach() to Author's attention, which in the simpler terms, is an "Array.map()"-like syntax for for-loop.
i.e.
The main benefit of taking
thisArray
as the last argument to lambda is the fact that you can encapsulate the behaviour in a named function (here a lambda is provided). Rest is pretty obvious.In the task I mentioned above for reversing strings and putting them in a new array, I agree that
forEach
is a good approach.I personally would still prefer
map()
overforEach()
becausenewArray
manuallymap()
returns an array. we can do further processing on array like filtering and sorting right there after runningmap()
without ever need to manipulatenewArray
.For example.
totally agree!