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

flatten (Functional Programming) es-toolkit

Creates a function that flattens nested arrays to a given depth. Use it with pipe.

const result = pipe(array, flatten(depth));
Info

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

Usage

flatten flattens the piped array up to depth. When depth is omitted, it flattens one level. It is lazy-capable for one-level flattening inside pipe.

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

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

Parameters

depthnumber, optionalrequired

The depth to flatten. Defaults to 1.

Returns

A function that maps a nested array to a flattened array.

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