GuobaGuoba Utils

counting

@guoba-ai/utils / array / counting

Function: counting()

function counting<T>(array, fn): Record<string, number>;

Defined in: array.ts:343

Count occurrences of each key derived from array items.

Parameters

array

T[]

The array to count

fn

(item) => string

Function that returns a key for each item

Returns

Record<string, number>

An object mapping keys to their occurrence count

Example

counting(['a', 'b', 'a', 'c', 'b', 'a'], v => v)
// { a: 3, b: 2, c: 1 }

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

counting([], () => 'key') // {}

On this page