Sub topic 3
Reaching into Nested Structures
This chapter assumes you already know basic array and object destructuring, defaults, rest, and spread from the earlier chapters. It also assumes you understand the difference between reference values and primitive values.
Nested Patterns
A pattern can contain other patterns, so it can follow the shape of the data. If an intermediate object might be missing, give it a default of {}. Without that default, reading a property of undefined throws a TypeError.
const order = { id: 1, customer: { name: 'Lin' } };
const { customer: { name }, shipping: { city } = {} } = order;
console.log(name); // 'Lin'
console.log(city); // undefinedSpread Copies Are Shallow
Spread copies only the top level. Nested objects are still shared between the copy and the original.
const defaults = { theme: 'light', layout: { cols: 2 } };
const settings = { ...defaults, theme: 'dark' };
settings.layout.cols = 3;
console.log(defaults.layout.cols); // 3Assigning to Existing Variables
You can destructure into variables that already exist. This lets you swap two values without a temporary variable. When a statement starts with an object pattern, wrap it in parentheses. Otherwise JavaScript reads the { as the start of a block.
let a = 1, b = 2;
[a, b] = [b, a];
let x;
({ x } = { x: 10 });
console.log(a, b, x); // 2 1 10Defaults vs null, and Errors
A default applies only when the value is undefined. If the value is null, the null is kept. Destructuring null or undefined itself throws a TypeError.
const { v = 5 } = { v: null };
console.log(v); // null
try { const { length } = null; } catch (e) { console.log(e instanceof TypeError); } // trueAny Iterable Works
Array patterns and array spread work with any iterable, including strings, Sets, and Maps. Each Map entry comes out as a [key, value] pair.
const [c1, c2] = 'hi';
console.log(c1, c2, [...new Set([1, 1, 2])]); // 'h' 'i' [1, 2]
for (const [k, val] of new Map([['ann', 90]])) console.log(k, val);Object Spread vs Object.assign
| Aspect | `{ ...a, ...b }` | `Object.assign(target, b)` |
|---|---|---|
| Result | A new object | Changes `target` in place |
| Setters on target | Not called; properties are defined | Called |
| Depth | Shallow | Shallow |
Use spread when you want a new value. Use Object.assign when you need to change an object that already exists.
Immutable Updates and Omitting Keys
To update a nested value without changing the original, spread at every level you change. To make a copy with one key removed, pull that key out and collect the rest.
const state = { user: { name: 'Ada', age: 36 }, token: 'abc' };
const updated = { ...state, user: { ...state.user, age: 37 } };
const { token, ...publicState } = state;
console.log(state.user.age, updated.user.age); // 36 37
console.log(publicState); // { user: { name: 'Ada', age: 36 } }Exercise
Given const { a: { b } = {} } = { a: null };, what happens, and why?
Show answer
It throws a TypeError. The default {} applies only when the value is undefined. Here a is null, so the pattern tries to destructure null.
Exercise
After const copy = { ...orig }; copy.list.push(1);, does orig.list change?
Show answer
Yes. Spread is shallow, so copy.list and orig.list refer to the same array.