Skip to the solution
MediumFrontend6 min read

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

677 views

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.

  • es2024
  • map-groupby
  • 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

6 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 silently merges two distinct object-key groups (coercion), while Map.groupBy correctly keeps them separate

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 71 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 nextMedium

Why is 0.1 + 0.2 not exactly 0.3?

Continue