Skip to main content

countBy

A function that counts how many times each element appears in an array and returns the result as an object.

If an iteratee is provided, each element is passed to the iteratee and the count is based on the returned value.


Code

🔗 View source code


Interface

typescript
// without iteratee
export function countBy<T extends readonly any[]>(
arr: T
): Record<T[number], number>;

// with iteratee
export function countBy<T extends readonly any[], K extends PropertyKey>(
arr: T,
iteratee: (value: T[number]) => K
): Record<K, number>;

Usage

typescript
import { countBy } from '@modern-kit/utils';

countBy([1, 2, 3, 2, 1]);
// { 1: 2, 2: 2, 3: 1 }

countBy([{ address: 'seoul' }, { address: 'incheon' }, { address: 'seoul' }], (value) => value.address);
// { seoul: 2, incheon: 1 }