← TechnologyThe essentials of

flat(): how many levels to remove

Publisher: ResumeMaker • 1 source

Published: 22 Oct 2026 13:13 · Updated: 22 Oct 2026 13:13

AI source check: 21 Oct 2026 12:40. This is not editorial approval.

In a few sentences

In brief

The flattening has a set depth and returns a new array.

Useful to know

flat() collects elements from nested arrays to a selected depth. By default it is one level : [1, [2, [3]]].flat() gives [1, 2, [3]]. With flat(2), the result is [1, 2, 3]. [1]

Select the depth according to the data structure. Indiscriminate removal of all levels can delete useful grouping, for example rows and columns. The new external array does not mean deep copying of every object inside; the method decides the list structure, not the independence of all data.

Sources

For further detail and to check the information:

  1. [1] MDN — Array.flat ↗