Page cover

๐Ÿ—บ๏ธArray.map()

Array.map()

.map

no mutation

.map

no mutation

Description

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

Array.prototype.map ( callbackfn [ , thisArg ] )

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Example

let array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

Description

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Example

Calls a function on each array element and returns the result as new array. Or feeds all hungry monkeys.

_Documentation on _MDN

map()

map method will create a new array of elements where each element is a value returned from the function provided. The example above shows the function provided doubling each element. Hence, mapArray is [2, 4, 6, 8].

Note: just like filter method, map will not mutate the original array because it will create a new array.

Last updated

Was this helpful?