What is the concept of Array.map?

前端 未结 8 1812
甜味超标
甜味超标 2020-11-30 06:36

I am having problems understanding the concept of Array.map. I did go to Mozilla and Tutorials Point, but they provided very limited info regarding this.

<
8条回答
  •  盖世英雄少女心
    2020-11-30 07:14

    Javascript map() Syntax

    arrayObj.map(callback[,context]);

    arrayObj is a original array on which map() is invoked.

    The map() takes 2 named arguments, First is a callback function and the second is a context object. callback function gets triggered on every element of an array.

    In addition, callback function takes 3 arguments:

    function callback(currentElement,index,array){

    }

    currentElement – This is a current elements of array which is passed to callback function

    index – Index of the current element

    array – complete array on which map() is applied

    Out of these 3 elements, currentElement parameter is mandatory while the rest 2 parameters are optional.

    However, map() does not change the original array, it creates a new array element which is generated by callback function.

    You can read more on JavaScript map function

提交回复
热议问题