Nested {#each} and Keyed Updates
Handle nested lists and ensure efficient DOM updates with keyed each blocks.
Nested {#each} and Keyed Updates is a free Sveltejs Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Dimensions
Nest {#each} blocks to render tables, grids, or trees.
{#each rows as row, i}
<tr>
{#each row as cell, j}
<td>{cell}</td>
{/each}
</tr>
{/each}Keys at Each Level
Add keys at every level for efficient updates.
{#each rows as row (row.id)}
{#each row.cells as cell (cell.id)}
...
{/each}
{/each}Tree Structures
Use recursion-friendly components or each blocks for tree-like data.
Avoid Index Keys
Using the array index as a key fails when items are inserted or removed in the middle. Prefer a stable id.
Mutable Data Trap
Mutating nested arrays without reassigning the outer reference breaks reactivity. Reassign at the top level.
rows = rows.map(r => r.id === id ? {...r, name} : r);Filtering and Sorting
Always return a new array when filtering or sorting; mutate-in-place will not trigger updates.
sorted = [...items].sort();Performance
Keys make adding, removing, or moving items O(n) instead of forcing full rerenders.
Reordering Lists
With keys, reordering animations are easy: combine with animate:flip.
Conditional Rendering Inside
Mix {#if} blocks inside each for fine control over which items show.
Component per Item
For complex items, render a child component to keep the loop body small.
{#each todos as todo (todo.id)}
<Todo {todo} />
{/each}Pagination Pattern
For long lists, paginate or virtualize rather than rendering thousands of nodes.
Quick Check
Why prefer stable IDs over indices for keys?
Recap
Nest each blocks for 2D data, key every level with stable IDs, and always reassign collections to trigger updates.
Frequently asked questions
Is the “Nested {#each} and Keyed Updates” lesson free?
Yes — the full text of “Nested {#each} and Keyed Updates” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.
What will I learn in “Nested {#each} and Keyed Updates”?
Handle nested lists and ensure efficient DOM updates with keyed each blocks. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Sveltejs Academy?
No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Nested {#each} and Keyed Updates” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Sveltejs Academy lesson?
Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- {#if} {#else if} {#else}
- {#each} with Index and Key
- {#key} for Forcing Re-render
- Nested {#each} and Keyed Updates