es-toolkit/docs/reference/array/flattenDeep.md
2024-07-12 09:56:21 +09:00

855 B

flattenDeep

Flattens all depths of a nested array.

It works the same as if you called Array#flat as flat(Infinity) in the JavaScript language, but it's faster.

Signature

// Utility type for recursively unpacking nested array types to extract the type of the innermost element
type ExtractNestedArrayType<T> = T extends ReadonlyArray<infer U> ? ExtractNestedArrayType<U> : T;

function flattenDeep<T>(arr: T[]): Array<ExtractNestedArrayType<T>>;

Parameters

  • arr (T[]): The array to flatten.

Returns

(Array<ExtractNestedArrayType<T>>): A new array with all depths flattened.

Examples

const array = [1, [2, [3]], [4, [5, 6]]];

const result = flattenDeep(array);
// Return [1, 2, 3, 4, 5, 6]