Use map() method in JavaScript Array

map() method helps you to create a new array by calling a function on each element of the Array. This function is a custom function which can be used in order to manipulate data within an array and get a new resultant array.

A simple implementation of map() over an Array. Let’s find the square of all the possible numbers in an array.

ES5 Implementation

var numbers = [3,4,22,533,6,52,1,34,454,2,435,456];

var numberSquareES5 = numbers.map(function(number){
return number*number;
});
console.log(numberSquareES5)

ES6 Implementation

var numbers = [3,4,22,533,6,52,1,34,454,2,435,456];

var numberSquareES6 = numbers.map(number=>number*number);
console.log(numberSquareES6)

This can be seen in action out here below.

Check out all the other posts on JavaScript Essentials to get a Grasp of these JavaScript Utilities.


Originally published at The Web Juice.

Like what you read? Give Shiv Kumar Ganesh a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.