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 to1.
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.
