Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Update Nested Data Safely

In the video, Ngozi swapped next week’s Tuesday and Thursday dinners. Each dinner sits in a meals array, inside a day record, inside the week, so the update copied every level on the way to each dinner and left the other five days shared with last week.

Key Points

  • A dinner is three arrows deep. lastWeek points at the week’s array, slot 1 points at Tuesday’s record, and its meals property points at Tuesday’s meals array. Those three objects are the path to the change, and last week reaches all of them. Change any of them in place, and last week changes too.
  • Copying only the day record with spread doesn’t protect the dinner. After const nextTuesday = { ...lastWeek[1] };, the copy’s meals still points at last week’s array, so nextTuesday.meals[1] = "bean tacos"; gives last Tuesday bean tacos too.
  • swapDinners() copies every level on the path. .with(1, ...) builds a new meals array with the other day’s dinner at position 1, where the dinner sits. The spread builds a new record around it, and a second .with() builds a new week with both new records in their slots. Nothing in it writes into an object it was given.
  • The days off the path stay shared. nextWeek[0] === lastWeek[0] is true, because the new week has new records only in slots 1 and 3. Earlier, .map() copied all seven days; here you copy only the path and share the rest, so the unchanged days stay the same objects. Sharing is safe only while no code writes into the shared records or their meals, so any later change to next week has to copy its path too.
  • The sharing helps when you compare. As long as every update copies its path, a day that’s still shared hasn’t changed, so === shows you which days were replaced: only Tuesday and Thursday are new. It compares arrows, not meals: a false doesn’t tell you whether the contents differ.
  • The rule: to change something nested, copy every level on the path to it, and share the rest.

Final Code

plan.js swaps two dinners in a copy of the week and leaves last week as it was:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last lesson, you gave Saturday a new cook with .map() and a spread record, and last week stayed as it was. That change sat at the top of a day record.

00:09 In this video, you’ll make a change one level deeper, inside a day’s meals.

00:15 Here’s plan.js. Last week’s seven day records sit at the top, folded into one line to save space. Ngozi wants to swap two dinners next week. Tuesday’s pepper soup moves to Thursday, and Thursday’s bean tacos move to Tuesday.

00:31 Each dinner sits in a meals array, inside a day record, inside the week. Next week needs the swap. Last week has to keep both dinners where they are.

00:43 Draw the arrows for Tuesday. The name lastWeek points at the week’s array. Its slot one points at Tuesday’s record. The record’s meals property points at Tuesday’s meals array, and the dinner is that array’s second item.

00:57 Thursday’s dinner sits the same way.

01:00 Those three objects are the path to the change: the week, the day, and the meals. Last week reaches every one of them. Change any of them in place, and last week changes too.

01:12 Watch out: when you copy only the day record with spread, the dinner isn’t protected. The copy is a new record, but its meals still points at last week’s array.

01:23 Write a dinner there, and last Tuesday gets bean tacos too.

01:29 So the update copies every level on the path. Here’s a function, swapDinners(), that takes a week and two positions. Its first two lines give the two day records short names.

01:41 The next line builds a new meals array with .with(). Its first argument, one, is the dinner’s position. The new array holds the other day’s dinner there.

01:51 Around it, the spread builds a new record whose meals points at the new array. The line after it does the same for the second day. Last, .with() builds a new week with both new records in their slots, and the function returns it.

02:07 Nothing in it writes into an object it was given.

02:11 Call it with positions one and three, for Tuesday and Thursday. Next Tuesday has bean tacos, and next Thursday has pepper soup. Last Tuesday still has pepper soup.

02:24 The last line asks whether both weeks hold the same Monday record. Your turn: pause the video and predict what it prints.

02:37 It prints true. The new week has new records only in slots one and three. Its other five slots point at last week’s records. Earlier, .map() copied all seven days.

02:50 Here, you copy only the path and share the rest, so the unchanged days stay the same objects. Sharing is safe only while no code writes into those records or their meals.

03:01 So any later change to next week has to copy its path too.

03:07 So here’s the rule: to change something nested, copy every level on the path to it, and share the rest. That’s what keeps last week safe. The update never writes into an object last week can reach.

03:21 It only builds new ones. And the sharing helps when you compare. As long as every update copies its path, a day that’s still shared hasn’t changed, so === shows you which days were replaced.

03:34 It only compares arrows, so a false doesn’t tell you whether the contents differ.

03:40 You’ve followed the arrows to one dinner, copied each level on the way, and left the other days shared. Next, in Exercise: Update Plans Without Mutating, you’ll write the planner’s update helpers for next week and check that last week stays as it was.

Become a Member to join the conversation.