Function groupBy

  • Groups values of an iterable or asynchronous iterable in an object based on the output of the grouper function.

    Example

    const groupByType = groupBy((value: number) => number % 2 === 0 ? "even" : "odd");
    groupByType([1, 2, 3, 4, 5]); // { even: [2, 4], odd: [1, 3, 5] }

    Type Parameters

    • Item

    • Key extends PropertyKey

    Parameters

    • grouper: Unary<Item, Key>

      Grouper function.

    Returns (<Iterable>(iterable) => ReducerOutput<Iterable, ReadOnlyRecord<Key, ReadOnlyArray<Item>>>)

    Object with grouped values.

      • <Iterable>(iterable): ReducerOutput<Iterable, ReadOnlyRecord<Key, ReadOnlyArray<Item>>>
      • Type Parameters

        • Iterable extends IsomorphicIterable<Item>

        Parameters

        • iterable: Iterable

        Returns ReducerOutput<Iterable, ReadOnlyRecord<Key, ReadOnlyArray<Item>>>