Sub topic 7
Updating Nested Data Immutably
This chapter assumes you already know how spread syntax makes shallow copies and how map and filter return new arrays. Here you combine those tools to change a value deep inside a data tree without touching the original.
The Problem with One Level of Spread
A single spread copies only the top level. Nested objects stay shared, so a change at depth still leaks into the original:
const state = { user: { name: 'Ada', address: { city: 'London' } } };
const wrong = { ...state };
wrong.user.address.city = 'Paris';
console.log(state.user.address.city); // 'Paris', the original changedwrong is a new object, but wrong.user is the same object as state.user. To change city safely, you must copy every object on the path from the root down to the value you change.
Copy the Path, Share the Rest
The rule is: spread each level along the changed path, and leave every other branch as it is.
const state = {
user: { name: 'Ada', address: { city: 'London' } },
settings: { theme: 'dark' }
};
const next = {
...state,
user: {
...state.user,
address: { ...state.user.address, city: 'Paris' }
}
};
console.log(state.user.address.city); // 'London'
console.log(next.settings === state.settings); // true, the unchanged branch is shared
console.log(next.user === state.user); // false, the changed path was copiedRead the update from the outside in. Each level spreads the old object and then overrides the one key that leads further down. settings is never mentioned, so the new state keeps a reference to the same settings object.
Updating an Item Inside a Nested Array
Real state often mixes objects and arrays. To change one item in an array, use map at the array level and spread at the object levels:
const board = {
title: 'Sprint',
columns: [
{ id: 'a', cards: [{ id: 1, done: false }, { id: 2, done: false }] },
{ id: 'b', cards: [{ id: 3, done: false }] }
]
};
const next = {
...board,
columns: board.columns.map(col =>
col.id !== 'a'
? col
: {
...col,
cards: col.cards.map(card =>
card.id === 2 ? { ...card, done: true } : card
)
}
)
};
console.log(board.columns[0].cards[1].done); // false
console.log(next.columns[0].cards[1].done); // true
console.log(next.columns[1] === board.columns[1]); // true, column b is reusedReturn the original item unchanged when it is not the target. That keeps references stable for untouched data.
Common Errors
| Mistake | Symptom | Fix |
|---|---|---|
| Spreading only the root | The original changes at depth | Spread every level on the path |
| Putting the override before the spread, as in `{ city: 'Paris', ...address }` | The old value overwrites the new one | Spread first, then override |
| Copying every item with `map(c => ({ ...c }))` | All references change, so change detection sees everything as new | Return unchanged items as they are |
| Pushing into a nested array (`col.cards.push(x)`) | Mutation of shared data | Use `cards: [...col.cards, x]` |
Exercise
Given const s = { a: { b: { c: 1 } }, d: { e: 2 } }, write an expression that returns a new state with c set to 5, leaves s unchanged, and keeps next.d === s.d true.
Show answer
const next = { ...s, a: { ...s.a, b: { ...s.a.b, c: 5 } } }; Each object on the path a → b is copied, and d is carried over by the root spread, so it stays the same reference.
Exercise
In the board example, why does the code return col instead of { ...col } for columns that are not the target?
Show answer
Returning col keeps the original reference, so unchanged columns stay shared with the old state. Copying them would allocate new objects for no reason and make reference-equality checks report them as changed.