Skip to the solution
EasyFrontend5 min read

How does Object.groupBy() work in ES2024 and when should you use it over reduce?

1.1k views

The problem

Question presented to candidate: "Before Object.groupBy existed, you'd write a reduce call to group items by a category. What does Object.groupBy actually give you that reduce doesn't β€” is it purely about being shorter to write?"

What a strong answer should cover:

  • πŸ“Œ Interview term: Object.groupBy(items, keyFn) β€” an ES2024 static method that groups an array's elements into a plain object, keyed by whatever keyFn returns for each element β€” genuinely equivalent in RESULT to the classic reduce-based grouping pattern, verified directly to produce an equal grouped structure.
  • πŸ“Œ Interview term: the real, direct answer to what's actually different β€” verified directly: the object Object.groupBy returns has a genuinely null prototype (Object.getPrototypeOf(result) is literally null) β€” NOT a normal {} object inheriting from Object.prototype β€” a real, deliberate safety choice specifically preventing a grouping key that happens to collide with an inherited property name (like "toString" or "constructor", covered in this bank's own Map/Set-vs-objects question) from causing a real bug.
  • πŸ“Œ Interview term: keys are still coerced to strings β€” verified directly: grouping by an object key genuinely still coerces it to the literal string "[object Object]", the identical real coercion this bank's own Map/Set-vs-objects question covers for plain object property keys β€” Object.groupBy genuinely does NOT fix that limitation, unlike Map.groupBy (covered in this bank's own dedicated comparison question).
  • A precise answer names that Object.groupBy is genuinely a one-purpose, single-call replacement specifically for the grouping pattern β€” verified directly producing an equivalent real result to a hand-written reduce, but without the manual initial-value/push boilerplate reduce requires.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly naming the null-prototype safety benefit (not just brevity) is the strong signal beyond "it's shorter than reduce."

Code / implementation expected: Yes β€” the real, direct proof of the null prototype plus a side-by-side reduce-based equivalent is the clearest demonstration of what's genuinely different, not just shorter.

  • es2024
  • array
  • object-groupby

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: Object.groupBy's result genuinely has a null prototype (unlike a reduce accumulator), and still coerces non-string keys

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 7 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is the difference between for...of and for...in?

Continue