Skip to content

flat ​

The flat function "flattens" an array of arrays into a single array up to the specified depth.

Syntax ​

typescript
flat<T>(array: any[], depth?: number): T[];

Parameters ​

NameTypeDescription
arrayany[]Source array
depthnumberFlattening depth (optional, default 1)

Returns ​

TypeDescription
T[]New flattened array

Examples ​

typescript
flat([1, [2, [3, [4]]]], 2); // => [1, 2, 3, [4]]
flat([1, [2, 3]]); // => [1, 2, 3]

Notes ​

  • If depth is not provided, the default is 1.
  • Does not modify the original array.

References ​

Released under the MIT License.