Master programming syntax, solve interactive challenges & run live code in Syntaxis Explore Arena →
Reference / JavaScript

Array.prototype.map()

Creates a new array populated with the results of calling a provided callback function on every element in the calling array.

Syntax
map(callbackFn: (element: T, index: number, array: T[]) => U, thisArg?: any): U[]

Parameters

NameTypeDescription
callbackFnFunctionFunction executed for each element. Receives (element, index, array).
thisArganyValue to use as this when executing callbackFn.

Examples

Transforming objects to IDs
const users = [{ id: 1, name: 'Alex' }, { id: 2, name: 'Taylor' }];
const ids = users.map(u => u.id);
console.log(ids); // [1, 2]
Does not mutate the original array. If you do not use the returned array, use forEach() or for...of instead.