I recently noticed that when using array.prototype.map(), my callback function may accept a third argument which is the array I'm mapping. I wonder if there are any use cases for this argument because inside the callback function, the array can be accessed anyways.
If I access the array I'm mapping inside the callback function, why/when should I rather use the third argument instead of just accessing the array?
Even though both methods should work fine in every use case I can imagine, I'd like to know which is the recommended way to do it and why.
myArr.filter(x => x % 2 == 1).map((x, i, arr) => x+arr.length)- it's a chained operation, so after the first chain, you can't refer tomyArras your working data. You can stop the chain, assign to a variable and use that but it's a bit wasteful. Case 2: your callback is in another scope to the array you havecallback = (x, i, arr) => x+arr.lengthin one place and you domyArr.map(callback)in another, so you cannot refer to the original array in the callback.