Sub topic 7
Using Objects and Other Non-String Values as Keys
This chapter assumes you can call Map.groupBy and read groups with get, has, and for...of. It also assumes you know that === compares objects by reference. Here you will see how Map.groupBy decides whether two keys are the same, which matters as soon as your keys are objects, numbers, or special values like NaN.
The rule: SameValueZero
A Map compares keys with an algorithm called SameValueZero. It works like === with one difference:
| Comparison | `===` | SameValueZero |
|---|---|---|
| `NaN` vs `NaN` | `false` | `true` |
| `0` vs `-0` | `true` | `true` |
| `1` vs `"1"` | `false` | `false` |
| `{}` vs `{}` (two literals) | `false` | `false` |
| `obj` vs the same `obj` | `true` | `true` |
Map.groupBy builds its result with this rule. If two callback return values are equal under SameValueZero, their elements land in the same group. Otherwise they get separate groups.
Primitive edge cases
Primitives keep their type, so 3 and "3" stay apart. NaN collapses into a single group, and -0 is stored as 0.
const readings = [3, NaN, -0, 0, NaN, "3"];
const byValue = Map.groupBy(readings, (x) => x);
console.log(byValue.size); // 4
console.log(byValue.get(NaN)); // [NaN, NaN]
console.log(byValue.get(0)); // [-0, 0]
console.log(byValue.get(3)); // [3]
console.log(byValue.get("3")); // ["3"]Object keys need shared references
An object key matches only the same object. To group by objects, return objects that already exist, such as entries from a lookup table, so every element in a category returns the identical reference.
const fruit = { label: "Fruit", aisle: 1 };
const meat = { label: "Meat", aisle: 7 };
const categoryOf = { bananas: fruit, cherries: fruit, goat: meat };
const inventory = [
{ name: "bananas", quantity: 0 },
{ name: "goat", quantity: 23 },
{ name: "cherries", quantity: 5 },
];
const byCategory = Map.groupBy(inventory, ({ name }) => categoryOf[name]);
for (const [category, items] of byCategory) {
console.log(category.label, category.aisle, items.length);
}
// Fruit 1 2
// Meat 7 1
console.log(byCategory.get(fruit).map((i) => i.name)); // ["bananas", "cherries"]The keys keep their data, so the loop can read category.aisle directly without a second lookup.
The fresh-object trap
If the callback creates a new object on each call, every element gets its own group, even when the objects look identical.
const inventory = [
{ name: "bananas", type: "fruit" },
{ name: "cherries", type: "fruit" },
];
const broken = Map.groupBy(inventory, ({ type }) => ({ type }));
console.log(broken.size); // 2, not 1
console.log(broken.get({ type: "fruit" })); // undefinedReading groups back
Lookups follow the same rule. Keep a reference to each key object (as fruit and meat above) if you plan to call get. If you don't have those references, iterate the Map with for...of, keys(), or entries() and inspect each key.
Exercise
What does this print, and why? const m = Map.groupBy([[1], [1], [2]], (a) => a); console.log(m.size, m.get([1]));
Show answer
It prints 3 undefined. Each array element is its own object, so the three keys are three distinct references and each one starts its own group. m.get([1]) creates yet another new array, which matches no key, so it returns undefined.
Exercise
You group log entries with Map.groupBy(logs, (e) => e.timestamp), where each timestamp is a Date object, and every entry ends up in its own group. Fix the callback so entries from the same calendar day share a group.
Show answer
Return a primitive built from the date, for example (e) => e.timestamp.toISOString().slice(0, 10). Strings are compared by value, so entries from the same day produce equal keys under SameValueZero.