JavaScript Array#map: index argument

前端 未结 3 1364
夕颜
夕颜 2021-02-02 06:15

My question is about the map method of arrays in JavaScript.

You can pass it a function that takes a second argument, the index of the current element of th

3条回答
  •  梦毁少年i
    2021-02-02 06:39

    Here is a description of of the map function:

    arr.map(callback[, thisArg])
    

    callback
    Function that produces an element of the new Array, taking three arguments:

    currentValue
    The current element being processed in the array.

    index
    The index of the current element being processed in the array.

    array
    The array map was called upon.

    The map function takes a callback function as argument (and not an index as argument, as originally stated in the question before it was edited). The callback function has an index as a parameter - the callback is called automatically, so you don't provide the index yourself. If you only need the current value, you can omit the other parameters.

提交回复
热议问题