Function flatten

  • Flattens nested arrays recursively with levels iterations.

    Remarks

    The verbose type signature comes from the recursive definition TypeScript uses for the built-in Array.flat().

    Example

    import * as A from "flurp/array";

    const flattenOneLevel = A.flatten();
    flattenOneLevel([[3, 4], [], [5, 6, 7, [8, 9]]]); // [3, 4, 5, 6, 7, [8, 9]]

    const flattenTwoLevels = A.flatten(2);
    flattenTwoLevels([[3, 4], [], [5, 6, 7, [8, 9]]]); // [3, 4, 5, 6, 7, 8, 9]

    Type Parameters

    • T

    Parameters

    • levels: number = 1

    Returns ((arr: readonly T[]) => FlatArray<T, 0 | 2 | 1 | -1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[])

      • (arr: readonly T[]): FlatArray<T, 0 | 2 | 1 | -1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
      • Parameters

        • arr: readonly T[]

        Returns FlatArray<T, 0 | 2 | 1 | -1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]