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

    Function arrFlatMap

    • The arrFlatMap() method returns a new array formed by applying a mapping function to each element and then flattening the mapped result by one level.

      This is equivalent to calling arrMap() followed by arrFlatten() with a depth of 1, but it avoids allocating the intermediate mapped array.

      Use this helper when each input item may produce zero, one, or multiple output items as part of the mapping step. The mapped value is flattened by exactly one level: if the callback returns an array, its elements are appended to the output; non-array values are appended directly.

      To flatten already-nested data without mapping, use arrFlatten() instead.

      callbackFn is invoked only for indexes of the array which have assigned values. Empty slots in sparse arrays are skipped.

      Type Parameters

      • T

        Identifies the type of array elements

      • R = T

        Identifies the type of the flattened result values, defaults to T.

      Parameters

      • theArray: ArrayLike<T>

        The array or array-like object to map and flatten.

      • callbackFn: ArrFlatMapCallbackFn<T, R>

        A function that is called for each existing element and may return either a single value or an array of values to flatten into the result.

      • OptionalthisArg: any

        The value to use as the this when executing the callbackFn.

      Returns R[]

      A new array containing the mapped and flattened values.

      0.14.0

      arrFlatMap([1, 2, 3], (value) => [value, value * 10]);
      // [1, 10, 2, 20, 3, 30]

      arrFlatMap(["one", "two"], (value) => value.split(""));
      // ["o", "n", "e", "t", "w", "o"]

      arrFlatMap({ length: 2, 0: "a", 1: "b" }, (value, index) => [index, value]);
      // [0, "a", 1, "b"]

      arrFlatMap([1, 2, 3, 4], (value) => value % 2 ? [value] : []);
      // [1, 3]

      arrFlatMap([1, 2], (value) => [[value, value + 10]] as any);
      // [[1, 11], [2, 12]]