Skip to solution
mediumFrontend

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

246 views
01

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

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: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every claim below was actually run with real, distinct object keys — not asserted from do

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, direct proof: Object.groupBy() silently merges distinct object keys into one group; Map.groupBy() keeps them genuinely distinct — 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 105 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track