JavaScript Map() Overview At a Glance

In this JavScript tutorial i will discuss about JavaScript map() function. The JavaScript map() method create a new array with the result of calling a provided function on every element in the calling array.

Map() calls a provided callback function once for each element in an array and return a new array from the result. If you don't understand the above quote then see the JavaScript map syntex.

 

MAP SYNTEX
Syntex :- map(callback(currentValue, index, array), arg);

 

See the below example of JavaScript map

var arr = [10, 20, 30, 40, 50];
var newArray = arr.map(function(value, index) {
    value++;
    return value;
});
console.log(newArray);

 

If you run this code it will return

OUTPUT
[11,21,31,41,51]

 

See another example of javascript map() function

var arr = [{
        price: "100",
        Model: "Primo"
    },
    {
        price: "300",
        Model: "A 50"
    }
];

var newArray = arr.map(function(value) {
    return value.price;
});

console.log(newArray);

 

Output of this code

OUTPUT
["100","300"]

 

Recommended : JavaScript Pass Function as Parameter with Example

 

Hope it can help you.