yta
    Preparing search index...

    Function groupBy

    • Collect the items of the iterator into a map of arrays keyed by the return value of your key generator function.

      Optionally pass a map function which will map the inner values.

      Note we return a Map which natively implements Symbol.iterator which means you are free to chain another operator after it’s consumed.

      import { pipe } from "yta";
      import { group-by, of } from "yta/sync";

      pipe(
      of(
      { name: "foo", value: 5 },
      { name: "foo", value: 42 },
      { name: "bar", value: 101 },
      { name: "foo", value: 13 },
      { name: "bar", value: 2 },
      ),
      groupBy(
      ({ name }) => name,
      ({ value }) => value,
      ),
      Object.fromEntries,
      );
      // => { "foo": [5, 42, 13], "bar": [101, 2] }

      See also:

      The native Map object

      Type Parameters

      • A
      • K
      • B = A

      Parameters

      • getKey: (item: A) => K
      • OptionalmapFn: (item: A) => B

      Returns (items: Iterable<A>) => Map<K, B[]>