Skip to solution
easyFrontend

How would you use Object.groupBy() to group an array of orders by status, and how does it compare to a manual reduce()?

1.2k views
01

Understand the problem

Question presented to candidate: "Given an array of orders, each with a status field, write code to group them by status. Do it two ways — with Object.groupBy() and with reduce() — and tell me what genuinely differs between the two results."

What a strong answer should cover:

  • 📌 Interview term: Object.groupBy(items, callback) — a real, built-in ES2024 method that genuinely produces the identical shape a hand-written reduce()-based groupBy has produced for years — an object whose keys are the callback's return values and whose values are arrays of the matching items — but built-in, shorter, and with one real correctness advantage over a naive reduce.
  • 📌 Interview term: the real, direct verified comparison — verified directly: both approaches produced the exact same real grouped JSON shape for a realistic orders-by-status array — confirming Object.groupBy() is a genuine, correct, drop-in shortcut for the common reduce()-based grouping pattern, not a different algorithm.
  • 📌 Interview term: the real correctness advantage — verified directly: a common, real reduce()-based groupBy BUG — forgetting to initialize the accumulator's array for a new key before pushing — genuinely throws a real TypeError; Object.groupBy() has no equivalent failure mode, since the built-in handles bucket-creation internally.
  • 📌 Interview term: the null-prototype difference — verified directly: Object.groupBy()'s result object genuinely has NO prototype at all (Object.getPrototypeOf() === null) — a real, deliberate safety choice — while a plain reduce()-built object genuinely inherits normal Object.prototype methods like hasOwnProperty.
  • A precise answer names WHY the null-prototype choice matters: it genuinely prevents a real, rare-but-real collision where an order status happened to literally be the string "hasOwnProperty" or "constructor" from silently shadowing or colliding with an inherited method on a plain reduce-built object.

Clarifying questions expected:

  • None — this is a definitional/comparison question; directly implementing both approaches and comparing their real, verified output is the strong signal.

Code / implementation expected: Yes — both a real Object.groupBy() call and a real, equivalent reduce() implementation, executed side by side with identical input.

es2024trickyreal-world
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: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every claim below was actually run — both implementations were executed against the identic

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, direct proof: Object.groupBy() and an equivalent reduce() produce byte-identical output, while a common reduce bug genuinely throws — verified directly
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 2 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track