Function: groupBy()
ts
function groupBy<T, K>(array, iteratee): Record<string, T[]>;Groups array items by a derived key. 按派生键对数组元素分组。
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of array items / 数组元素类型 |
K extends PropertyKey | The group key type / 分组键类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | The source array / 源数组 |
iteratee | GroupByIteratee<T, K> | The property name or function used to derive the group key / 用于生成分组键的属性名或函数 |
Returns
Record<string, T[]>
An object of grouped items / 分组结果对象
Example
ts
groupBy([{ type: 'a' }, { type: 'b' }, { type: 'a' }], 'type')
// => { a: [{ type: 'a' }, { type: 'a' }], b: [{ type: 'b' }] }Since
1.2.0