Aller au contenu principal

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 }));