Skip to main content

countBy()

countBy<T>(array, iteratee): Record<string, number>

Counts elements by a key derived from each element.

note

Keys are always strings (JavaScript object behavior).


Type Parametersโ€‹

T: Tโ€‹

The type of elements in the array.


Parametersโ€‹

array: readonly T[]โ€‹

The array to iterate over.

iteratee: (value) => string | numberโ€‹

A function that returns the grouping key for each element.


Returns: Record<string, number>โ€‹

An object with keys and their occurrence counts.


Sinceโ€‹

2.0.0


Performanceโ€‹

O(n) time & space, uses for loop to avoid array method overhead.


Also known asโ€‹

count (Modern Dash) ยท countBy (Lodash, es-toolkit, Remeda, Ramda) ยท counting (Radashi) ยท โŒ (Effect, Antfu)


Exampleโ€‹

countBy([1.2, 1.8, 2.1, 2.9], Math.floor);
// => { '1': 2, '2': 2 }

countBy(
[{ age: 25 }, { age: 30 }, { age: 25 }],
(u) => u.age
);
// => { '25': 2, '30': 1 }

How it works?โ€‹

Groups elements by a computed key and counts occurrences instead of collecting elements.

With iteratee functionโ€‹


Use Casesโ€‹

Count items by status/category ๐Ÿ“Œโ€‹

Count occurrences of each status or category in a collection. Essential for dashboards, admin panels, and e-commerce analytics.

const orders = [
{ id: 1, status: "pending" },
{ id: 2, status: "shipped" },
{ id: 3, status: "pending" },
];

countBy(orders, (order) => order.status);
// => { pending: 2, shipped: 1 }

Count occurrences in simple arraysโ€‹

Count frequency of each value in a flat array. Useful for surveys, polls, and frequency analysis.

const responses = ["yes", "no", "yes", "yes", "no"];

countBy(responses, (r) => r);
// => { yes: 3, no: 2 }

Count by computed keyโ€‹

Count items grouped by a derived or calculated value. Useful for segmentation and conditional grouping.

const users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 17 },
{ name: "Charlie", age: 32 },
];

countBy(users, (user) => (user.age >= 18 ? "adult" : "minor"));
// => { adult: 2, minor: 1 }

Display badge counts in navigationโ€‹

Compute counts per category to display as badges in a sidebar or nav bar. Universal pattern for any app with "Inbox (3)", "Pending (12)", or "Errors (5)".

const tickets = [
{ id: 1, status: "open" },
{ id: 2, status: "open" },
{ id: 3, status: "closed" },
{ id: 4, status: "in-progress" },
{ id: 5, status: "open" },
];

const counts = countBy(tickets, (t) => t.status);
// => { open: 3, closed: 1, "in-progress": 1 }

// Render in sidebar
// ๐Ÿ“ฌ Open (3)
// ๐Ÿ”„ In Progress (1)
// โœ… Closed (1)