How to group an array in JavaScript — in one line instead of twelve

You ask an AI: "show the orders sorted into groups by status." You get twelve lines of reduce, an empty object, a check for "does this key exist yet," and a push. It works. But reading it is like deciphering someone else's handwriting.
Yet since 2024, JavaScript has built-in grouping. In every modern browser and in Node.js from version 21. Same task — one line. Out of old habit, models often don't use it, so it helps to know what to ask for.
Let's go from a raw list to finished groups.
Step by step
The input is a plain array of orders:
const orders = [
{ id: 1, userId: 42, status: "new", total: 1200, date: "2026-09-02" },
{ id: 2, userId: 7, status: "shipped", total: 800, date: "2026-09-15" },
{ id: 3, userId: 42, status: "new", total: 450, date: "2026-08-28" },
{ id: 4, userId: 7, status: "done", total: 3100, date: "2026-08-03" },
]
-
Decide what you're grouping by. It's the "which basket does this go in?" question. Here the answer is the
statusfield. The answer must always be a single value: a string or a number. -
Call
Object.groupBy. First argument — the array, second — a function that names the basket for each element:const byStatus = Object.groupBy(orders, order => order.status)The function here is an ordinary callback:
groupBycalls it for every order itself. -
Look at the result. You get an object where the key is the group name and the value is an array of its elements:
{ new: [ {id: 1, ...}, {id: 3, ...} ], shipped: [ {id: 2, ...} ], done: [ {id: 4, ...} ], }You get a group by name:
byStatus.new. If a group doesn't exist, you getundefined, not an empty array. So writebyStatus.cancelled ?? []. -
Group by something computed, not just a field. The function can return anything. For example, the month from a date:
const byMonth = Object.groupBy(orders, o => o.date.slice(0, 7)) // { "2026-09": [...], "2026-08": [...] }Or a price range:
o => o.total > 1000 ? "large" : "small". -
Compute something per group. After grouping, you usually want totals — how many and for how much:
for (const [month, list] of Object.entries(byMonth)) { const sum = list.reduce((acc, o) => acc + o.total, 0) console.log(month, list.length, sum) } -
If the key isn't a string, use
Map.groupBy. An object turns every key into text. When you group by a number, a date or an object, use a Map — it keeps keys as they are:const byUser = Map.groupBy(orders, o => o.userId) byUser.get(42) // orders of user 42
Two traps
The result is a "bare" object. Object.groupBy returns an object with no prototype. So byStatus.hasOwnProperty("new") crashes with "is not a function." Check the new way: Object.hasOwn(byStatus, "new") or just "new" in byStatus.
An old environment. Browsers from before 2024 and Node.js 20 and below don't have the method — you'll get Object.groupBy is not a function. Then you need a fallback with reduce:
const byStatus = orders.reduce((groups, o) => {
(groups[o.status] ??= []).push(o)
return groups
}, {})
It's those same twelve lines from the AI, just squeezed into four.
How to ask the AI
group the orders by statusGroup the orders array by the status field using Object.groupBy (not reduce). Return an object shaped status → array of orders. For each group, compute the count and the sum of total. Treat a missing group as an empty array. The project runs on Node 22.The weak prompt almost always gets you a dozen lines of reduce. The strong one names the method, the shape of the result and the runtime version — so the model doesn't have to guess.
What you'll get
Instead of a wall of reduce, one clear line you'll still be able to read a month from now. And on screen — orders sorted by status, with totals. From this you can build a kanban board, a monthly report or a "show only new" filter in an evening.
How is groupBy different from filter?
filter pulls out one group and throws away the rest. groupBy sorts the whole list into all the baskets in one pass. Need every group — groupBy. Need one — filter.
Is the order inside a group preserved?
Yes. Elements land in a group in the same order they had in the original array. Want them sorted — sort before grouping, or inside each group.
How do I group in Python?
The simplest way is defaultdict(list) from the collections module. Careful with itertools.groupby: it only merges neighboring elements, so without sorting first your groups get split into pieces.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





