Sub topic 5
Working with a Null-Prototype Result
This chapter assumes you have already called Object.groupBy, as covered in the earlier chapters. You should also be comfortable with plain objects, the in operator, optional chaining (?.), and nullish coalescing (??).
Object.groupBy does not return an ordinary object. Its result has a null prototype, which means it inherits nothing from Object.prototype. Most of the time you won't notice. It does matter when your code expects methods that every normal object inherits.
What is missing
A plain object literal such as {} inherits hasOwnProperty, toString, valueOf, constructor, and other members. A grouped result has none of them. The only properties it has are the group keys you created.
const groups = Object.groupBy([1, 2, 3], (n) => (n > 1 ? "big" : "small"));
console.log(Object.getPrototypeOf(groups)); // null
console.log(typeof groups.hasOwnProperty); // "undefined"
console.log(typeof groups.toString); // "undefined"
console.log(Object.keys(groups)); // ["small", "big"]Why the result is built this way
With a normal object, a group key can collide with an inherited name. Suppose you group words by the word itself. On a plain object, "constructor" in result is true before you add anything, so presence checks and ??= initialisation give wrong answers. A null prototype removes that whole class of bug.
const words = ["toString", "map", "constructor", "map"];
const groups = Object.groupBy(words, (w) => w);
console.log(groups.constructor); // ["constructor"]
console.log(groups.map); // ["map", "map"]
console.log("valueOf" in groups); // false
const plain = {};
console.log("constructor" in plain); // true, inherited from Object.prototypeCommon errors
| Code | What happens | Fix |
|---|---|---|
| `groups.hasOwnProperty("big")` | TypeError: not a function | `Object.hasOwn(groups, "big")` |
| `` `${groups}` `` or `String(groups)` | TypeError: cannot convert object to primitive value | `JSON.stringify(groups)` |
| `groups.missing.length` | TypeError: cannot read properties of undefined | `groups.missing?.length ?? 0` |
The last error isn't caused by the null prototype. It still trips people up often, because a group exists only when at least one element produced its key.
Safe ways to read groups
Use tools that don't depend on inherited methods:
const groups = Object.groupBy([1, 2, 3], (n) => (n > 1 ? "big" : "small"));
console.log(Object.hasOwn(groups, "big")); // true
console.log("medium" in groups); // false
console.log(groups.medium?.length ?? 0); // 0
console.log(groups.medium ?? []); // []
for (const [key, items] of Object.entries(groups)) {
console.log(key, items.length); // "small" 1, then "big" 2
}
console.log(JSON.stringify(groups)); // {"small":[1],"big":[2,3]}Object.keys, Object.entries, Object.values, for...in, destructuring, and JSON.stringify all work normally, because they never call a method on the object itself.
Logging
Node.js prints these objects as [Object: null prototype] { small: [ 1 ], big: [ 2, 3 ] }. That label is informational. It doesn't mean anything went wrong.
Exercise
You run const g = Object.groupBy(["a", "b"], (s) => s); and then if (g.hasOwnProperty("a")) console.log("yes");. What happens, and how do you rewrite the check so it works?
Show answer
It throws a TypeError, because g has a null prototype and hasOwnProperty is undefined on it. Write if (Object.hasOwn(g, "a")) console.log("yes"); or if ("a" in g) console.log("yes"); instead. Either one logs "yes".
Exercise
Why is Object.groupBy(words, (w) => w).constructor safe to read as a group, when the same lookup on a reduce result built from {} might return the Object function?
Show answer
The groupBy result inherits nothing. constructor is only present if some element produced the key "constructor", and then it holds that group's array. An object built from {} inherits constructor from Object.prototype, so a missing group would still return the Object function.