countBy es-toolkit
Categorizes the elements of an array based on a transformation function result and returns an object with counts.
Usage
countBy(arr, mapper)
Use countBy when you want to categorize array elements by a specific criterion and count each group. It groups elements using the value returned by the transformation function as a key and calculates the number of elements in each group.
You can also count based on a specific property of an object array.
Parameters
The array to count elements from.
A function that returns the value to use as the categorization criterion, called with each item, its index, and the array.
Returns
An object representing how many elements exist for each categorization criterion.
Examples
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.