How to use javascript map method

map() method apply a function for every element on the array and creates a new array with the result of every element.


array.map(function(currentValue, index, arr), thisValue)

The function is invoked with three arguments: the value of the element, the index of the element (optional), and the current Array (optional) .

Example:


var numbers = [1, 4, 9];

var doubles = numbers.map(function(num) {
  return num * 2;
});

In this example the array double is equal to [2, 8, 18] and the array number still [1, 4, 9].

Leave a comment

Create a website or blog at WordPress.com

Up ↑