For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/fp/groupBy.md.

groupBy (Functional Programming) es-toolkit

Creates a function that groups values by a key. Use it with pipe.

const result = pipe(array, groupBy(getKey));
Info

Prefer the original es-toolkit groupBy in ordinary code. Use this fp variant when composing transformations with pipe.

Usage

groupBy calls getKey for each value in the piped array and returns an object whose keys are the returned keys and whose values are arrays of matching items.

import { groupBy, pipe } from 'es-toolkit/fp';

pipe(
  ['one', 'two', 'three'],
  groupBy(word => word.length)
); // => { 3: ['one', 'two'], 5: ['three'] }

Parameters

getKey(item: T) => Krequired

The function that returns the group key for each value.

Returns

A function that maps a readonly T[] to grouped arrays by key.

(array: readonly T[]) => Record<K, T[]>
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.