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.
Map
Symbol.iterator
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] } Copy
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
Optional
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
Mapwhich natively implementsSymbol.iteratorwhich means you are free to chain another operator after it’s consumed.See also:
The native
Mapobject