flattenDepth

array

Recursively flatten array up to depth times.

Signature

flattenDepth<T>(array: any[], depth?: number): any[]

Parameters

NameTypeDescription
arrayany[]The array to flatten
depth?numberThe maximum recursion depthDefault: 1

Returns

any[] - Returns the new flattened array

Examples

Flatten to depth 1

import { flattenDepth } from 'dashlite'

const array = [1, [2, [3, [4]], 5]]
const result = flattenDepth(array, 1)
console.log(result)

Output:

[1, 2, [3, [4]], 5]

Flatten to depth 2

import { flattenDepth } from 'dashlite'

const array = [1, [2, [3, [4]], 5]]
const result = flattenDepth(array, 2)
console.log(result)

Output:

[1, 2, 3, [4], 5]

Available since version 1.0.0