Object.groupBy replaces the reduce you keep writing
Grouping an array by a key has been a five-line reduce for a decade. It is one line now.
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 plus and every current browser.
javascript