flattenDepth

Modified on Mon, 07 Aug 2023

Description

Recursively flatten array up to depth times.

Syntax

flattenDepth(array, depth)

Parameters

Input ParameterTypeDescription
arrayArrayThe array to flatten.
depthnumberThe maximum recursion depth.

Return value

Type
Array

Sample

ExampleResultDescription
flattenDepth([1, [2, [3, [4]]]], 1)[1, 2, [3, [4]]]Returns the new flattened array for depth 1
flattenDepth([1, [2, [3, [4]]]], 2)[1, 2, 3, [4]]Returns the new flattened array for depth 2
flattenDepth([1, [2, [3, [4]]]], 3)[1, 2, 3, 4]Returns the new flattened array for depth 3

To view all the List functions, click here.

See Also