Function: keyBy()
ts
function keyBy<T, K>(array, iteratee): Record<string, T>;Creates an object that maps derived keys to items. 按派生键创建元素映射对象。
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of array items / 数组元素类型 |
K extends PropertyKey | The key type / 键类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | The source array / 源数组 |
iteratee | KeyByIteratee<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