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
| Name | Type | Description |
|---|---|---|
| callbackFn | Function | Function executed for each element. Receives (element, index, array). |
| thisArg | any | Value 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.