Skip to content

flat ​

The flat() method flattens a nested array up to the requested depth and returns a new array containing the unrolled elements.

Interactive example ​

Syntax ​

typescript
function flat<
	const GenericInput extends readonly unknown[],
	const GenericDepth extends number = 1
>(
	input: GenericInput,
	depth?: GenericDepth
): FlatArray<GenericInput, GenericDepth>[]

Parameters ​

  • input: The potentially nested array to flatten.
  • depth: The number of levels to unroll. Defaults to 1.

Return value ​

A new array in which the elements have been concatenated up to the requested depth. If the input is a tuple, the resulting type precisely reflects the flattening level.

See also ​

  • flatMap - Transforms then flattens the result by one level
  • map - Applies a function to each element

Sources ​

Released under the MIT license.