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

chunk (Functional Programming) es-toolkit

Creates a function that splits an array into sub-arrays of a given length. Use it with pipe.

const result = pipe(array, chunk(size));
Info

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

Usage

chunk divides an array into sub-arrays, each of length size. When the array cannot be divided evenly, the final chunk holds the remaining elements.

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

pipe([1, 2, 3, 4, 5], chunk(2)); // => [[1, 2], [3, 4], [5]]

// A size larger than the array yields a single chunk.
pipe([1, 2, 3], chunk(10)); // => [[1, 2, 3]]

Parameters

sizenumberrequired

The length of each chunk. Must be a positive integer.

Returns

A function that maps a readonly T[] to an array of chunks.

(array: readonly T[]) => T[][]

Throws

Throws an error if size is not a positive integer.

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.