Skip to content

Function: keyBy()

ts
function keyBy<T, K>(array, iteratee): Record<string, T>;

Creates an object that maps derived keys to items. 按派生键创建元素映射对象。

Type Parameters

Type ParameterDescription
TThe type of array items / 数组元素类型
K extends PropertyKeyThe key type / 键类型

Parameters

ParameterTypeDescription
arrayreadonly T[]The source array / 源数组
iterateeKeyByIteratee<T, K>The property name or function used to derive the key / 用于生成键的属性名或函数

Returns

Record<string, T>

An object whose keys map to items / 键到元素的映射对象

Example

ts
keyBy([{ id: 1, name: 'A' }], 'id')
// => { '1': { id: 1, name: 'A' } }

Since

1.2.0

Released under the MIT License.