Skip to the solution
MediumFrontend6 min read

When would you reach for Map.groupBy() instead of Object.groupBy(), and what problem does it solve with non-string keys?

246 views

The problem

Question presented to candidate: "You are grouping calendar events by the Date object representing their start day. You reach for Object.groupBy() and every event ends up in ONE group. What went wrong, and how would you fix it?"

What a strong answer should cover:

  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: Object.groupBy() uses its grouping key as a real OBJECT property key, which means any non-string, non-symbol key (like a Date object, or any plain object) is genuinely coerced to a string first β€” and every distinct object genuinely coerces to the SAME string, "[object Object]" β€” silently merging every group into one.
  • πŸ“Œ Interview term: Map.groupBy() β€” the real fix β€” groups into a genuine Map, which (covered in more depth in this bank's own dedicated Map-vs-object question) can use ANY value, including an object or a Date instance, as a real, distinct key with no coercion at all β€” verified directly, two distinct object keys stayed genuinely separate, with correct per-key group counts.
  • πŸ“Œ Interview term: the real decision rule β€” a precise answer names the actual decision criterion: if the grouping key is (or ever COULD be) a non-string, non-symbol value β€” an object, a Date, a class instance β€” reach for Map.groupBy(); if the key is always a plain string (a status label, a category name), Object.groupBy()'s plain-object result is simpler to consume with dot-access and JSON.stringify.
  • A precise answer names that this bank's own dedicated Object.groupBy-vs-Map.groupBy question covers the core ES2024 mechanism and null-prototype-result distinction in depth β€” this question's own value-add is the specific, real, silent-data-loss FAILURE MODE with non-string keys, and the practical "which one do I reach for" decision it drives.
  • A precise answer names the fix does not require restructuring the grouping logic at all β€” only swapping Object.groupBy(items, fn) for Map.groupBy(items, fn), since both share an identical callback signature.

Clarifying questions expected:

  • None β€” this is a definitional/practical question; directly diagnosing the prompt's own described bug (verified via real reproduction) is the strong signal.

Code / implementation expected: Yes β€” a real, direct reproduction of the merging bug with distinct object keys, and the Map.groupBy() fix, both genuinely executed.

  • es2024
  • tricky
  • real-world

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, direct proof: Object.groupBy() silently merges distinct object keys into one group; Map.groupBy() keeps them genuinely distinct β€” verified directly

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 105 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

What are computed property names?

Continue