Function default

  • Recursively flatten array up to depth times.

    Parameters

    • array: any[]

      The array to flatten.

    • depth: number = 1

      The maximum recursion depth.

    Returns any[]

    Returns the new flattened array.

    Since

    5.4.0

    See

    [[flatMap]],[[flatMapDeep]],[[flatMapDepth]],[[flattenDeep]]

    Example

    const array = [1, [2, [3, [4]], 5]]

    flattenDepth(array, 1)
    // => [1, 2, [3, [4]], 5]

    flattenDepth(array, 2)
    // => [1, 2, 3, [4], 5]