mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
f85579bc39
* fix(countBy): Support readonly array in `countBy` * fix(countBy): Support numeric and symbol keys in `countBy`
733 B
733 B
countBy
Count the occurrences of each item in an array based on a mapper
function.
Signature
function countBy<T, K extends PropertyKey>(arr: T[], mapper: (item: T) => K): Record<K, number>;
Parameters
arr
(T[]
): The input array to count occurrences.mapper
((item: T) => K
): The transformation function that maps each item to a key.
Returns
(Record<K, number>
) An object containing the count of each item based on the transformation function.
Examples
import { countBy } from 'es-toolkit/array';
const array = [1, 2, 3, 4, 5, 6];
const result = countBy(array, x => (x % 2 === 0 ? 'even' : 'odd'));
console.log(result);
// Output: { 'odd': 3, 'even': 3 }