Function: countBy()
ts
function countBy<T>(collection, iteratee): Record<string, number>;Counts items grouped by an iteratee key. 按 iteratee 分组并统计每组数量。
Type Parameters
| Type Parameter | Description |
|---|---|
T | Item type / 元素类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
collection | readonly T[] | Source array / 源数组 |
iteratee | keyof 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