Skip to content

countBy

Groups the values of a Set by a derived key and counts how many fall into each group.

Available since v3.0.3

import { countBy } from '@helpers4/set';
// or, from the all-in-one package (same code, one install):
import { countBy } from 'helpers4/set';
countBy<T, R>(set: ReadonlySet<T>, fn: function): Map<R, number>
ParameterTypeDescription
setReadonlySet<T>The Set to summarize
fnfunctionFunction deriving the grouping key from each value

Map<R, number> — A Map from derived key to the number of values in that group

Groups values by a derived key and counts how many fall into each group.

countBy(new Set([1, 2, 3, 4]), v => v % 2 === 0 ? 'even' : 'odd')
// => Map(2) { 'odd' => 2, 'even' => 2 }

View source on GitHub