3

I am pulling an input elements array from the DOM using the the id and using .map() to loop through the array. My code works, however currentValue and index are switched:

ports = $("#edit_display_control_ports > li > input").map(function(currentValue, index){ 
  return index.value;}
).toArray(); 

Again, this code works. According to documentation, the first parameter of the map() function should be the currentValue and the second, index. However I have to switch the two. The currentValue return the index of the array, while the index parameter returns the element.

bbnewey
  • 33
  • 3
  • 1
    [Why are jQuery's callback arguments inconsistent?](https://stackoverflow.com/questions/3612320) – adiga Oct 24 '19 at 15:26

1 Answers1

3

You are partly correct, your issue is that you are not running javascript's native .map , this is jQuerys .map, since you are calling it on a jQuery collection.

jQuery's map works differently:

callback Type: Function( Object elementOfArray, Integer indexInArray ) => Object The function to process each item against. The first argument to the function is the array item, the second argument is the index in array The function can return any value. A returned array will be flattened into the resulting array. Within the function, this refers to the global (window) object.

Take a look here

MKougiouris
  • 2,821
  • 1
  • 16
  • 19