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/chunkBy.md.

chunkBy (Functional Programming) es-toolkit

Creates a function that splits adjacent values whenever a key changes. Use it with pipe.

const result = pipe(array, chunkBy(iteratee));
Info

This helper is specific to es-toolkit/fp. Use it when you want this operation as part of a pipe pipeline.

Usage

chunkBy walks the piped array from left to right and groups adjacent values while iteratee returns the same key. A new chunk starts when the key changes.

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

pipe(
  [1, 1, 2, 2, 1],
  chunkBy(value => value)
); // => [[1, 1], [2, 2], [1]]

Parameters

iteratee(value: T) => unknownrequired

The function that returns the grouping key for each value.

Returns

A function that maps a readonly T[] to chunks of adjacent values.

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