Skip to content

Function: countBy()

ts
function countBy<T>(collection, iteratee): Record<string, number>;

Counts items grouped by an iteratee key. 按 iteratee 分组并统计每组数量。

Type Parameters

Type ParameterDescription
TItem type / 元素类型

Parameters

ParameterTypeDescription
collectionreadonly T[]Source array / 源数组
iterateekeyof T | ((item) => PropertyKey)Property name or key getter / 属性名或 key 获取函数

Returns

Record<string, number>

Key to count map / 分组计数对象

Example

ts
countBy([{ type: 'a' }, { type: 'b' }, { type: 'a' }], 'type')
// => { a: 2, b: 1 }

Since

1.2.0

Released under the MIT License.