groupBy<K,E,>(source: Iterable<E> | AsyncIterable<E>,keySelector: (element: E) => K | Promise<K>,): Promise<Map<K, E[]>>
Groups elmenets of an async interable source according to a specified
keySelector function and creates a map of each group key to the elements in
that group. Key values are compared using the === operator.
import { groupBy } from "./unique.ts"; interface IdName { id: number; name: string; } async function* gen(): AsyncIterableIterator<IdName> { yield { id: 1, name: "foo" }; yield { id: 2, name: "bar" }; yield { id: 3, name: "bar" }; yield { id: 4, name: "foo" }; } const map = await groupBy<string, IdName>(gen(), o => o.name); console.log(map);
The above example will print the following:
Map { "foo" => [ { id: 1, name: "foo" }, { id: 4, name: "foo" } ], "bar" => [ { id: 2, name: "bar" }, { id: 3, name: "bar" } ] }