Sub topic 12
Groups Share Element References
This chapter assumes you know how Object.groupBy builds its result and how JavaScript separates primitive values from objects. You should also know that assigning an object to a variable copies a reference, not the object.
What groupBy Creates and What It Reuses
Each call to Object.groupBy or Map.groupBy creates two kinds of new things: the result container (a null-prototype object or a Map) and one new array per group. It does not create new elements. Each group array holds the same values that were in the source iterable.
For primitives such as numbers and strings, this makes no difference, because they cannot be changed in place. For objects, it means the source array and the groups point at the same objects in memory.
const people = [{ name: "Ana" }, { name: "Ben" }];
const byInitial = Object.groupBy(people, (p) => p.name[0]);
byInitial.A[0].name = "Anna";
console.log(people[0].name); // "Anna"
console.log(byInitial.A[0] === people[0]); // trueThe strict equality check returns true because both expressions refer to one object. A change made through either path shows up in the other.
The Arrays Themselves Are Independent
Changing the group array itself, by adding, removing, or reordering entries, does not touch the source array. Only changes to the shared elements carry over.
const people = [{ name: "Ana" }, { name: "Ben" }];
const byInitial = Object.groupBy(people, (p) => p.name[0]);
byInitial.A.push({ name: "Ava" });
byInitial.B.length = 0;
console.log(people.length); // 2
console.log(byInitial.A.length); // 2
console.log(people[1].name); // "Ben"Common Mistake: Expecting Groups to Update
Keys are computed once, when groupBy runs. If you later change a property the key was based on, the element stays in its old group.
const people = [{ name: "Ana" }, { name: "Ben" }];
const byInitial = Object.groupBy(people, (p) => p.name[0]);
people[0].name = "Bea";
console.log(byInitial.A[0].name); // "Bea"
console.log(byInitial.B.length); // 1Now "Bea" sits under A. The result is a snapshot of how elements were grouped. It is not a live index. If the grouping property changes, run groupBy again.
Getting Independent Copies
If you need groups you can change without side effects, copy the elements before grouping. A shallow spread is enough for flat objects:
const people = [{ name: "Ana" }, { name: "Ben" }];
const byInitial = Object.groupBy(
people.map((p) => ({ ...p })),
(p) => p.name[0]
);
byInitial.A[0].name = "Anna";
console.log(people[0].name); // "Ana"For nested objects, use structuredClone on each element instead of a spread. Clone the elements rather than the result: structuredClone(groups) returns an ordinary object with Object.prototype, so the null-prototype behavior is lost.
| Approach | Group arrays | Elements | Cost |
|---|---|---|---|
| `groupBy(items, fn)` | New | Shared | Lowest |
| `groupBy(items.map(p => ({ ...p })), fn)` | New | Shallow copies | One object per element |
| `groupBy(items.map(structuredClone), fn)` | New | Deep copies | Highest |
Map.groupBy behaves the same way. Its group arrays are new, and its elements are shared.
Exercise
Given const items = [{ n: 1 }, { n: 2 }]; const g = Map.groupBy(items, (x) => x.n > 1);, what do items[1].n and items.length print after running g.get(true)[0].n = 99; g.get(false).pop();?
Show answer
items[1].n prints 99 because g.get(true)[0] is the same object as items[1]. items.length prints 2 because pop only removes the entry from the new group array, not from items.
Exercise
You group orders by status, then set order.status = "shipped" on an order in the "pending" group. Which group is the order in afterward, and how do you fix that?
Show answer
The order stays in the "pending" group because keys are computed only when groupBy runs. Call groupBy again on the source data to get groups that match the new status.