Question presented to candidate: "Before Object.groupBy existed, you'd write a reduce call to group items by a category. What does Object.groupBy actually give you that reduce doesn't — is it purely about being shorter to write?"
What a strong answer should cover:
- 📌 Interview term:
Object.groupBy(items, keyFn)— an ES2024 static method that groups an array's elements into a plain object, keyed by whateverkeyFnreturns for each element — genuinely equivalent in RESULT to the classic reduce-based grouping pattern, verified directly to produce an equal grouped structure. - 📌 Interview term: the real, direct answer to what's actually different — verified directly: the object
Object.groupByreturns has a genuinelynullprototype (Object.getPrototypeOf(result)is literallynull) — NOT a normal{}object inheriting fromObject.prototype— a real, deliberate safety choice specifically preventing a grouping key that happens to collide with an inherited property name (like"toString"or"constructor", covered in this bank's own Map/Set-vs-objects question) from causing a real bug. - 📌 Interview term: keys are still coerced to strings — verified directly: grouping by an object key genuinely still coerces it to the literal string
"[object Object]", the identical real coercion this bank's own Map/Set-vs-objects question covers for plain object property keys —Object.groupBygenuinely does NOT fix that limitation, unlikeMap.groupBy(covered in this bank's own dedicated comparison question). - A precise answer names that
Object.groupByis genuinely a one-purpose, single-call replacement specifically for the grouping pattern — verified directly producing an equivalent real result to a hand-writtenreduce, but without the manual initial-value/push boilerplate reduce requires.
Clarifying questions expected:
- None — this is a definitional/technical question; directly naming the null-prototype safety benefit (not just brevity) is the strong signal beyond "it's shorter than reduce."
Code / implementation expected: Yes — the real, direct proof of the null prototype plus a side-by-side reduce-based equivalent is the clearest demonstration of what's genuinely different, not just shorter.