
The problem you keep re-solving
Grouping a list by some property is one of the most repeated patterns in frontend code: split orders by status, bucket todos by completed, partition users by role. The old reflex is a reduce:
const byStatus = orders.reduce((acc, o) => {
(acc[o.status] ??= []).push(o);
return acc;
}, {});
It works, but it is noisy, easy to get wrong (the ??= initialization alone trips people up), and it silently produces an object that inherits from Object.prototype — so a status literally named "toString" or "constructor" would collide with built-ins.
ES2024 ships Object.groupBy and Map.groupBy natively. No lodash, no custom helper.
Object.groupBy
const orders = [
{ id: 1, status: "shipped" },
{ id: 2, status: "pending" },
{ id: 3, status: "shipped" },
{ id: 4, status: "cancelled" },
];
const byStatus = Object.groupBy(orders, (o) => o.status);
// {
// shipped: [{ id: 1, ... }, { id: 3, ... }],
// pending: [{ id: 2, ... }],
// cancelled: [{ id: 4, ... }],
// }
The callback returns the key; the runtime builds the groups for you. Cleaner to read, and the key can be any primitive (string, number, symbol, even true/false).
The gotcha: it returns a null-prototype object
Object.groupBy does not give you a normal {}. It returns an object with null as its prototype. That is intentional — it prevents the "toString" collision problem — but it changes how you interact with the result:
const groups = Object.groupBy(orders, (o) => o.status);
groups.hasOwnProperty("shipped"); // ❌ TypeError: groups.hasOwnProperty is not a function
"shipped" in groups; // ✅ true
Object.keys(groups); // ✅ ["shipped","pending","cancelled"]
Object.prototype.hasOwnProperty.call(groups, "shipped"); // ✅ true
If you need prototype methods, wrap it: Object.assign({}, groups) or Object.setPrototypeOf(groups, Object.prototype).
Map.groupBy when keys are not strings
If your grouping key is an object, a Date, or a non-coercible value, Object.groupBy will stringify it and everything collapses into one bucket. Use Map.groupBy instead — its keys keep their original identity and order:
const users = [
{ name: "Ada", team: { id: 1, name: "Core" } },
{ name: "Linus", team: { id: 2, name: "Kernel" } },
{ name: "Grace", team: { id: 1, name: "Core" } },
];
const byTeam = Map.groupBy(users, (u) => u.team);
byTeam.get(users[0].team).length; // 2 (Ada + Grace, same team object)
A Map also preserves insertion order and lets you iterate with .entries(), .forEach(), or for...of without the null-prototype awkwardness.
Other things worth knowing
- The groups are arrays of the original references (shallow). Mutating an item mutates it everywhere it appears — same as
reducewould. - The callback’s second argument is the index, in case your key depends on position.
- The result is always a fresh object/Map; the input array is never modified.
- Support: Chrome/Edge 117+, Firefox 119+, Safari 17.4+, Node 21+. For older targets, transpile or keep your
reducehelper.
When to reach for it
Use Object.groupBy for quick, read-only grouping by a string/number key. Reach for Map.groupBy when keys are objects or you want ordered, Map-native iteration. If you were about to install or import a grouping utility, you probably do not need it anymore.
// The whole "reduce" boilerplate above becomes one line:
const byStatus = Object.groupBy(orders, (o) => o.status);