Skip to main content

Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
94%
Published
a day ago (1.12.0)
function countBy
countBy<T>(
arr: T[],
mapper: (item: T) => string,
): Record<string, number>

Count the occurrences of each item in an array based on a transformation function.

This function takes an array and a transformation function that converts each item in the array to a string. It then counts the occurrences of each transformed item and returns an object with the transformed items as keys and the counts as values.

Type Parameters

  • The type of the items in the input array.

Parameters

arr: T[]
  • The input array to count occurrences.
mapper: (item: T) => string
  • The transformation function that maps each item to a string key.

Return Type

Record<string, number>

An object containing the transformed items as keys and the counts as values.

Add Package

deno add @es-toolkit/es-toolkit

Import symbol

import { countBy } from "@es-toolkit/es-toolkit";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { countBy } from "@es-toolkit/es-toolkit";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { countBy } from "@es-toolkit/es-toolkit";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { countBy } from "@es-toolkit/es-toolkit";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { countBy } from "@es-toolkit/es-toolkit";