Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 1

Nested destructuring in JavaScript

22 Nested destructuring

hard

From one order object, reach two levels down for a buyer's area, into an array of lines for the first item, and rename at depth. Then confirm what nesting does not bind, and what happens when a middle object is missing.

Stuck? Three hints, smallest first
  1. 1. A pattern nests wherever the data nests: { buyer: { address: { area } } }.
  2. 2. Where the data has an array, the pattern has an array, and a bare comma still skips an element.
  3. 3. Nesting binds only the leaves. The intermediate names are part of the path, not variables.

The answer

const attempt = (fn) => {
  try { return fn(); } catch (e) { return e.constructor.name; }
};

const order = {
  id: 7,
  buyer: { name: 'Emeka', address: { area: 'Yaba', city: 'Lagos' } },
  lines: [
    { item: 'rice', qty: 2 },
    { item: 'beans', qty: 1 },
  ],
};

const { buyer: { name, address: { area } } } = order;
const { lines: [{ item: firstItem, qty: firstQty }] } = order;
const { lines: [, { item: secondItem }] } = order;
const { buyer: { name: buyerName } } = order;

const missingMiddle = attempt(() => {
  const { seller: { name: sellerName } } = order;
  return sellerName;
});
const { seller: { name: guardedSeller } = {} } = order;

console.log('two levels down:', name, area);
console.log('into an array  :', firstItem, firstQty);
console.log('skipping one   :', secondItem);
console.log('renamed deep   :', buyerName);
console.log('buyer bound?   :', typeof buyer, '<- the path is not a variable');
console.log('missing middle :', missingMiddle);
console.log('guarded        :', guardedSeller);

Output


      
8 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('a nested pattern reaches the leaf directly', [name, area], ['Emeka', 'Yaba']);
test('an array inside an object takes an array pattern', [firstItem, firstQty], ['rice', 2]);
test('and a bare comma still skips an element at depth', secondItem, 'beans');
test('renaming works at any depth', buyerName, 'Emeka');
test('the intermediate name is a path, not a binding', typeof buyer, 'undefined');
test('a missing middle object throws', missingMiddle, 'TypeError');
test('a default on that object makes the read safe', guardedSeller === undefined, true);
test('and the source object is never touched', order.buyer.address.city, 'Lagos');

The fifth assertion is the one that surprises people: const { buyer: { name } } = order gives you name and not buyer. If you want both, ask for both — const { buyer, buyer: { name } } = order is legal and does exactly that.

The sixth is the risk. Each level of nesting is another property read, so one absent middle object throws a TypeError with a message about the leaf, not about the level that was actually missing. Past two levels, either put = {} defaults on the path or read it in steps; a five-level pattern over data you did not create is a crash waiting for a bad payload.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.