Skip to content

Function: map()

ts
function map<T, R>(collection, iteratee): T extends readonly unknown[] ? R[] : Record<string, R>;

Maps over an array or object and returns a transformed collection of the same shape. 对数组或对象执行映射,并返回相同结构的转换结果。

Type Parameters

Type ParameterDescription
T extends Record<string, unknown> | readonly unknown[]The source collection type / 源集合类型
RThe mapped value type / 映射值类型

Parameters

ParameterTypeDescription
collectionTThe source collection / 源集合
iteratee(value, key, collection) => RThe mapping function / 映射函数

Returns

T extends readonly unknown[] ? R[] : Record<string, R>

The mapped collection / 映射后的集合

Example

ts
map({ a: 1, b: 2 }, (value) => value * 2)
// => { a: 2, b: 4 }

Since

1.2.0

Released under the MIT License.