Skip to main content
Version: 2.x

API Reference > @easyops-cn/brick-next-pipes > countBy

countBy() function

统计一个集合中每个元素的指定属性(或迭代器执行结果)的值出现的次数。

Collection

Signature:

export declare function countBy(
collection: unknown[] | Record<string, unknown>,
iteratee: string | number
): Record<string, number>;

Parameters

ParameterTypeDescription
collectionunknown[] | Record<string, unknown>数据集合。
iterateestring | number属性名(或迭代器)。

Returns:

Record<string, number>

统计结果。

Remarks

透传调用 _.countBy

Example

countBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': 1, '6': 2 }

countBy(["one", "two", "three"], "length");
// => { '3': 2, '5': 1 }