Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ES6's Map.forEach iterate with 'value, key' instead of 'key, value'?

As shown on MDN, Map's forEach callback is called with value first, and then key. E.g.:

map.forEach(function(value, key, map) { ... })

Seems like key, value is a lot more common than value, key. Even the Map constructor expects an array of [key, value] pairs.

like image 902
ericsoco Avatar asked Oct 13 '15 19:10

ericsoco


1 Answers

It's probably just for laziness sake. Most forEach loops will only care about the value itself. By supplying it as the first parameter, you can construct a function that only accepts one parameter:

map.forEach(function (value) { /* do something with value */; })

Instead of

map.forEach(function (unused, value) { /* do something with value */; })
like image 186
Mr. Llama Avatar answered Oct 11 '22 11:10

Mr. Llama