Skip to content

Instantly share code, notes, and snippets.

@Gergling
Last active April 9, 2026 09:46
Show Gist options
  • Select an option

  • Save Gergling/ced247ca176eb2ac39fe2b68a545185b to your computer and use it in GitHub Desktop.

Select an option

Save Gergling/ced247ca176eb2ac39fe2b68a545185b to your computer and use it in GitHub Desktop.
A simple aggregation function for handling lists of data.
const aggregate = <
Item,
AggregationKey extends KeyType
>(items: Item[], keyFunc: (item: Item) => AggregationKey) => {
const map = new Map<AggregationKey, Item[]>();
items.forEach((item) => {
const key = keyFunc(item);
const list = map.get(key) || [];
map.set(key, [...list, item]);
});
return map;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment