Skip to solution
mediumFrontend

What is the difference between `Object.groupBy()` and `Map.groupBy()` — keys, prototypes and ordering?

677 views
01

Understand the problem

Question presented to candidate: "If you need to group a list of items by their category, and each category is represented by an OBJECT (not a string), which of Object.groupBy or Map.groupBy would actually work correctly, and which would silently produce a wrong result?"

What a strong answer should cover:

  • 📌 Interview term: Object.groupBy(items, keyFn) — returns a plain, null-prototype object (covered in more depth in this bank's own dedicated question) — its keys are genuinely coerced to strings, exactly like any plain object property key.
  • 📌 Interview term: Map.groupBy(items, keyFn) — returns a real Map instance instead — its keys are genuinely not coerced at all, accepting any value, including an object, as a real, distinct key.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: grouping by an object category key with Object.groupBy genuinely coerces EVERY object key to the identical literal string "[object Object]", silently merging what should have been separate groups into one — a real, silent data-corruption bug. Map.groupBy on the identical input genuinely keeps the object key as a real, distinct key, correctly preserving separate groups.
  • 📌 Interview term: return type consequences — a precise answer names the practical downstream differences: Object.groupBy's result works with Object.keys()/for...in/dot-or-bracket access, while Map.groupBy's result requires .get()/.has()/for...of — the identical real API differences this bank's own Map/Set-vs-objects/arrays question covers generally, just applied specifically to the two grouping functions.
  • A precise answer names that both genuinely share the identical grouping LOGIC and callback signature — the only real differences are the coercion behavior and the resulting container type, not the grouping algorithm itself.

Clarifying questions expected:

  • None — this is a definitional/comparison question; directly answering the prompt's own object-category scenario (naming which one silently breaks) is the strong signal.

Code / implementation expected: Yes — the real, side-by-side object-key grouping test — showing Object.groupBy silently merging groups while Map.groupBy correctly keeps them separate — is the clearest, most convincing demonstration.

es2024map-groupbyobject-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 and coercion claim below was actually run in Node (v21+ required; verified

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: Object.groupBy silently merges two distinct object-key groups (coercion), while Map.groupBy correctly keeps them separate
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 71 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track