Array.prototype.map()

Defination

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

Syntax

1
var new_array = arr.map(function callback(currentValue, index, array) {
2
// Return element for new_array
3
}[, thisArg])
Copied!

Examples

1
var numbers = [1, 5, 10, 15];
2
var doubles = numbers.map(function(x) {
3
return x * 2;
4
});
5
// doubles is now [2, 10, 20, 30]
6
// numbers is still [1, 5, 10, 15]
7
8
var numbers = [1, 4, 9];
9
var roots = numbers.map(Math.sqrt);
10
// roots is now [1, 2, 3]
11
// numbers is still [1, 4, 9]
Copied!

Usage

1
const numbers = [1, 2, 3, 4, 5];
2
const listItems = numbers.map((number) =>
3
<li>{number}</li>
4
);
Copied!

References