Function flatMap

  • Maps each element to an Iterable and flattens the result.

    Type Parameters

    • T

      Type of items in the source.

    • O

      Type of items in the resulting Iterable.

    Parameters

    • mapper: ((value: T, index: number) => Iterable<O>)

      Mapping function.

        • (value, index): Iterable<O>
        • Parameters

          • value: T
          • index: number

          Returns Iterable<O>

    Returns OperationFunction<T, O>

    Operation that maps each element to an Iterable and flattens the result.

    const result = flatMap<number, number>((value) => [value, value + 1])([1, 2, 3]);
    console.log([...result]); // [1, 2, 2, 3, 3, 4]