Skip to content

.tip

· one of the small things

Object.groupBy replaces the reduce you keep writing

For a decade, grouping an array by a key meant a five-line reduce. Now it's one line.

const byYear = Object.groupBy(posts, (p) => new Date(p.createdAt).getFullYear());
// { 2025: [...], 2026: [...] }

Map.groupBy does the same with a Map, which keeps non-string keys and insertion order. Both are in Node 21 and later, and in every current browser.

javascript