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/array/flatMapDeep.md.

flatMapDeep es-toolkit

Transforms each element of an array with a function's return value, then flattens all depths and returns a new array.

const result = flatMapDeep(arr, iteratee);

Usage

flatMapDeep(arr, iteratee)

Use flatMapDeep when you want to transform an array while completely flattening all nested arrays. First, it applies a function to each element, then flattens the resulting array to all depths.

import { flatMapDeep } from 'es-toolkit/array';

// Duplicate each element twice and flatten completely.
const result1 = flatMapDeep([1, 2, 3], item => [item, item]);
// Returns: [1, 1, 2, 2, 3, 3]

It flattens all depths no matter how deeply nested.

import { flatMapDeep } from 'es-toolkit/array';

// Flatten nested arrays completely.
const result = flatMapDeep([1, 2, 3], item => [[item, item]]);
// Returns: [1, 1, 2, 2, 3, 3]

// Flatten multiple levels of nesting.
const result2 = flatMapDeep([1, 2, 3], item => [[[item, item]]]);
// Returns: [1, 1, 2, 2, 3, 3]

Parameters

arrT[]required

The array to transform.

iteratee(item: T, index: number, array: readonly T[]) => Urequired

The function that transforms each array element. It receives the element, its index, and the array.

Returns

Returns a new array where each element is transformed and all depths are flattened.

Array<ExtractNestedArrayType<U>>

Examples

// Using index parameter
const arr = [1, 2, 3];
flatMapDeep(arr, (item, index) => [[item + index]]);
// Returns: [1, 3, 5]

// Using array parameter
flatMapDeep(arr, (item, _index, array) => [[item * array.length]]);
// Returns: [3, 6, 9]
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.