map()
map<
T,Result>(array,iteratee):Result[]
Creates an array of values by running each element in array through iteratee.
DEPRECATED
Use array.map(callback) directly instead.
Reason:
Native equivalent method now available
Type Parametersโ
T: Tโ
The type of elements in the input array.
Result: Resultโ
The type of elements in the output array.
Parametersโ
array: T[]โ
The array to iterate over.
iteratee: (value, index, array) => Resultโ
The function invoked per iteration.
Returns: Result[]โ
A new mapped array.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
map (Lodash, es-toolkit, Remeda, Ramda, Effect) ยท โ (Radashi, Modern Dash, Antfu)
Exampleโ
const numbers = [1, 2, 3, 4, 5];
// โ Deprecated approach
const doubled = map(numbers, x => x * 2);
console.log(doubled); // [2, 4, 6, 8, 10]
// โ
Recommended approach
const doubledNative = numbers.map(x => x * 2);
console.log(doubledNative); // [2, 4, 6, 8, 10]
How it works?โ
Creates an array of values by running each element through iteratee.
Deprecated: Use array.map() directly.
Native Equivalentโ
// โ map(arr, fn)
// โ
arr.map(fn)
Use Casesโ
Transform array elements ๐โ
Apply a function to each element.
const numbers = [1, 2, 3, 4];
numbers.map(n => n * 2);
// => [2, 4, 6, 8]
Extract property valuesโ
Get a specific property from each object.
const users = [{ name: "Alice" }, { name: "Bob" }];
users.map(u => u.name);
// => ["Alice", "Bob"]
Convert data formatโ
Transform data into a different structure.
const items = [{ id: 1, value: "a" }, { id: 2, value: "b" }];
items.map(({ id, value }) => ({ key: id, data: value }));