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.

      import { pipe } from "yta";
      import { groupBy, of } from "yta/async";

      await pipe(
      of(5, 42, 101, 13, 2)
      groupBy((n) => (n % 2 === 0 ? "even" : "odd")),
      async (entries) => Object.fromEntries(await entries),
      );
      // => { "odd": [5, 101, 13], "even": [42, 2] }

      See also:

      The native Map object

      Type Parameters

      • A
      • K
      • B = A

      Parameters

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

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