@nevware21/ts-utils
    Preparing search index...

    Function arrFlatten

    • Function

      The arrFlatten() method returns a new array with all sub-array elements flattened up to the specified depth (default 1).

      Use this helper when the input already contains nested arrays and you only need to control how deeply to flatten. Flattening is depth-based and only applies to values that are arrays; non-array values are copied into the output unchanged.

      For map-then-flatten workflows, use arrFlatMap() which always flattens mapped results by exactly one level.

      Type Parameters

      • T

        Identifies the base type of array elements

      Parameters

      • theArray: ArrayLike<any[] | T>

        The array or array-like object to flatten

      • Optionaldepth: number

        The flattening depth, defaults to 1. Use Infinity for complete flattening

      Returns any[]

      A new flattened array

      0.14.0

      arrFlatten([1, [2, 3], [4, [5, 6]]]);           // [1, 2, 3, 4, [5, 6]]
      arrFlatten([1, [2, 3], [4, [5, 6]]], 2); // [1, 2, 3, 4, 5, 6]
      arrFlatten([1, [2, 3], [4, [5, 6]]], Infinity); // [1, 2, 3, 4, 5, 6]
      arrFlatten([1, 2, 3]); // [1, 2, 3]
      arrFlatten([]); // []

      // With array-like objects
      arrFlatten({ length: 2, 0: 1, 1: [2, 3] }); // [1, 2, 3]