Skip to content

Function: groupBy()

ts
function groupBy<T, K>(array, iteratee): Record<string, T[]>;

Groups array items by a derived key. 按派生键对数组元素分组。

Type Parameters

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

Parameters

ParameterTypeDescription
arrayreadonly T[]The source array / 源数组
iterateeGroupByIteratee<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

Released under the MIT License.