A reduce with an accumulator object, written for the ninth time, replaced by a method.
// before
const byStatus = orders.reduce((acc, o) => {
(acc[o.status] ??= []).push(o)
return acc
}, {})
// ES2024
const byStatus = Object.groupBy(orders, (o) => o.status)
// and the Map version, for non-string keys
const byCustomer = Map.groupBy(orders, (o) => o.customer)
Object.groupBy returns an object with a null prototype, which is the detail that catches people — hasOwnProperty is not on it, and neither is anything else, which is safer and surprising. Map.groupBy is the one to reach for when the key is an object, because object keys on a plain object stringify to [object Object] and silently collapse.