Skip to solution
easyFrontend

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

1.1k views
01

Understand 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.

es2024arrayobject-groupby
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every grouping result and prototype check below was actually run in Node (v21+ required;

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

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

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 7 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track