Type of elements within the source iterable.
Type returned by keySelector and used to organise groups.
Type of the accumulated value created for each group.
The source iterable.
Selector that derives the grouping key for each element.
Either an initial accumulator value applied to every group or a factory invoked with the group key to produce that value.
Function that merges the current accumulator with the next element in the group.
OptionalkeyComparator: EqualityComparator<TKey, TKey>Optional equality comparator used to match group keys.
A sequence containing one key-value pair per group and its aggregated result.
When seedSelector is a factory function, it is evaluated once per group to obtain the initial accumulator.
const products = [
{ name: 'Apple', category: 'Fruit', price: 1.2 },
{ name: 'Banana', category: 'Fruit', price: 0.5 },
{ name: 'Carrot', category: 'Vegetable', price: 0.8 },
{ name: 'Broccoli', category: 'Vegetable', price: 1.5 },
];
const totalPriceByCategory = aggregateBy(
from(products),
p => p.category,
0,
(acc, p) => acc + p.price
).toArray();
console.log(totalPriceByCategory);
// [
// { key: 'Fruit', value: 1.7 },
// { key: 'Vegetable', value: 2.3 }
// ]
Groups elements by a computed key and aggregates each group by applying an accumulator within that group.