While the interface of .map()
method is .map( callback(index, domElement) )
, it's $.map( array, callback(elementOfArray, indexInArray) )
for $.map()... Any idea over the reason why $.map() choose to place the returned arguments in an order such as value-index?
Because the API is imperfect. It started out inconsistent, but fixing that now would break existing code that uses $.map()
.
If you study the jQuery API you will notice that all methods that work on a set of selected elements and accept callbacks, such as .each
, .html
, .text
, etc., all pass the index of the element as first argument, i.e. .map
is in line here. Usually you access the current element with this
inside the callback, this is just a common pattern in jQuery and so the developers might have decided that it is more important to have the index as first argument.
On the other hand, the native Array.prototype.map
method passes the value of element as first argument to the callback, so it seems to make sense that $.map
works the same way, since it is supposed process a generic set of items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With