Group array items with Object.groupBy
Group orders by status with Object.groupBy, read its null-prototype result, or use Map.groupBy to group by object identity.
You have a list of orders and need them bucketed by status for a dashboard. Use Object.groupBy to get one array per status. Compare it with this reducer, using the same orders array shown below.
const byStatus = orders.reduce((groups, order) => {
const key = order.status
if (!groups[key]) groups[key] = []
groups[key].push(order)
return groups
}, {})With the sample orders below, the reducer collects orders 1 and 3 under paid. Its plain object can collide with inherited names such as constructor.
One call with Object.groupBy
Object.groupBy(items, callback) calls the callback once per item and puts the item in the array for whatever key it returns. It has been Baseline across browsers since March 2024 and works in Node 22.
const orders = [
{ id: 1, status: 'paid', total: 40 },
{ id: 2, status: 'refunded', total: 15 },
{ id: 3, status: 'paid', total: 22 },
{ id: 4, status: 'pending', total: 9 },
]
const byStatus = Object.groupBy(orders, (order) => order.status)
console.log(Object.keys(byStatus)) // [ 'paid', 'refunded', 'pending' ]
console.log(byStatus.paid.map((order) => order.id)) // [ 1, 3 ]The paid bucket preserves the orders’ input order. The keys in this example appear in first-seen order. Array-index keys follow ordinary object enumeration rules and come first in numeric order.
The callback can compute the key, so (o) => (o.total >= 20 ? 'large' : 'small') splits the list by size. The arrays hold the original objects. byStatus.paid[0] === orders[0] is true, so changes to that order are visible through both references.
What differs from the reduce
The result has a null prototype, so grouping a status called constructor works the same way as grouping paid. Lookups, Object.keys, Object.entries and JSON.stringify all work, but byStatus.hasOwnProperty('paid') throws because there is no Object.prototype to inherit it from. Use Object.hasOwn(byStatus, 'paid') or 'paid' in byStatus instead.
The keys are strings or symbols. A returned symbol stays a symbol. Returning a boolean in JavaScript produces the keys 'true' and 'false', though TypeScript requires the callback to return a property key.
In TypeScript every group is optional, because the compiler cannot know which keys the data will produce. With strict on, byStatus.paid.map(...) fails with error TS18048: 'byStatus.paid' is possibly 'undefined', so write (byStatus.paid ?? []).map(...). The declarations ship in the es2024 lib; include that lib with your other library declarations if your target’s defaults omit it.
Objects as keys need Map.groupBy
If these plain owner objects go through Object.groupBy in JavaScript, both become the string '[object Object]' and share one bucket. Map.groupBy keeps the key as it is.
const alice = { name: 'Alice' }
const bob = { name: 'Bob' }
const tasks = [
{ title: 'Write tests', owner: alice },
{ title: 'Fix login', owner: bob },
{ title: 'Ship it', owner: alice },
]
const byOwner = Map.groupBy(tasks, (t) => t.owner)
console.log(byOwner.get(alice).map((t) => t.title))
// [ 'Write tests', 'Ship it' ]The Map holds one entry per owner object, and get with the same reference returns that owner's tasks.
Pick Object.groupBy when you want property lookups with string or symbol keys, and Map.groupBy when the key is an object or you want Map methods like has and size.