Skip to main content

core.countby

Home > @medplum/core > countBy

countBy() function

Counts iterable elements by a key function, returning a sparse map of key to count. Matches Lodash countBy semantics (no zero-fill for missing keys).

Signature:

export declare function countBy<T, K extends keyof any>(items: Iterable<T>, keyFn: (value: T) => K): Partial<Record<K, number>>;

Parameters

Parameter

Type

Description

items

Iterable<T>

The items to count.

keyFn

(value: T) => K

The function to extract the key from each item.

Returns:

Partial<Record<K, number>>

A map of key to count for keys that appear at least once.