Element type produced by the source iterable.
Key type generated by the key selector.
Source iterable enumerated to populate the lookup.
Selector invoked with each element and index to produce a key.
OptionalmapFactory: MapFactory<TKey>Optional factory used to construct the internal map for lookups.
A deferred iterable emitting grouped iterables keyed by the selector output.
const groups = [..._groupBy(
["ant", "bat", "apple"],
(word) => word[0]
)].map((group) => ({ key: group.key, values: [...group] }));
console.log(groups);
// [
// { key: "a", values: ["ant", "apple"] },
// { key: "b", values: ["bat"] }
// ]
or using the curried version:
const groups = pipeInto(
[
{ category: "fruit", item: "apple" },
{ category: "fruit", item: "banana" },
{ category: "veg", item: "carrot" }
],
groupBy((entry) => entry.category)
);
const shaped = [...groups].map((group) => ({
key: group.key,
items: [...group].map((entry) => entry.item)
}));
console.log(shaped);
// [
// { key: "fruit", items: ["apple", "banana"] },
// { key: "veg", items: ["carrot"] }
// ]
Groups elements from the source iterable by key and returns lazy grouped iterables for each bucket.