Hacker News new | ask | show | jobs
by Orou 2213 days ago
The issue doesn't arise because of parseInt() being able to take more than one argument, the issue is that map() is passing not only the value in the array but also its index as well as a copy of the full array.

I use map() all the time but I didn't know about that until I read this article. I wonder why map() functions this way in Javascript and if any other languages pass additional values like this in their own map() implementations.

I do think GP has a fair point of criticism - at least when I use map() I expect it to take each value in the array and pass it to the callback function. I don't expect it to take each value in the array, the index of that value and the entire array and pass all of that to the callback function instead.

3 comments

> I wonder why map() functions this way in Javascript and if any other languages pass additional values like this in their own map() implementations.

Sometimes it's very convenient to be able to look ahead or behind the current element being map()'d to make decisions.

Where do your expectations come from though?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

> map() is passing not only the value in the array but also its index as well as a copy of the full array. I use map() all the time but I didn't know about that until I read this article

Then you don't know the basics of the language you're using- how can you complain about it?

> when I use map() I expect it to take each value in the array and pass it to the callback function. I don't expect...

map is well documented. Making the element's index available is indeed pretty useful (the entire array less so, but sometimes it can be).