Sub topic 7
Choosing between for:each and iterator
This chapter helps you pick the right list directive for a given layout. It assumes you know basic HTML and JavaScript, including arrays and map. It also assumes you know how an LWC template and its JavaScript class fit together, how to bind a property with curly braces, and how to render conditionally. You should also have seen both for:each and iterator render a list with a key.
Both directives do the same core job
for:each and iterator both loop over an array and render the template block once per item. Both need a stable, unique key on the first element inside the loop. Both re-render when you assign a new array to the tracked property. The difference is what the template can read about each pass.
| Feature | `for:each` | `iterator` |
|---|---|---|
| Current item | `for:item="contact"` gives `contact` | `it.value` |
| Position | Optional `for:index="position"` | `it.index` |
| First item flag | Not available | `it.first` |
| Last item flag | Not available | `it.last` |
| Typical use | Plain lists, cards, rows | Lists that treat the ends differently |
Start with for:each
Most lists need only the item, and sometimes its position. For these, for:each is shorter and easier to read.
<template>
<ul>
<template for:each={contacts} for:item="contact">
<li key={contact.Id}>{contact.Name}</li>
</template>
</ul>
</template>Use for:index when you need the position for display or a data attribute. Do not use it as the key, because the position changes when items are added, removed or reordered.
Switch to iterator for first and last
for:each has no flag for the first or last item. When the markup must differ at the ends, iterator gives you first and last without extra JavaScript.
<template>
<ul>
<template iterator:it={contacts}>
<li key={it.value.Id}>
<span lwc:if={it.first}>First: </span>
{it.value.Name}
<span lwc:if={it.last}> (last)</span>
</li>
</template>
</ul>
</template>Typical cases are a header above the first row, a separator between items but not after the last, or a "Total" line after the last item.
A decision flow
Do not choose by key behavior
The two directives follow the same key rule. The key goes on the first element inside the loop, and it must be unique among siblings. With iterator, the key reads from it.value, for example key={it.value.Id}. Writing key={it.Id} is a common mistake, because it is the iterator object and the item is in it.value.
Consider JavaScript for other differences
If you need to mark ends in a for:each list, you can precompute a flag in a getter with map. This keeps the template simple, but it adds code to maintain.
get markedContacts() {
return this.contacts.map((contact, index, all) => ({
...contact,
isFirst: index === 0,
isLast: index === all.length - 1
}));
}Prefer iterator when the first and last checks are the only reason for preparing data. Prefer the getter when you already compute other per-item values, such as a CSS class, because the extra flags cost almost nothing there.
Rules of thumb
- Default to
for:each. - Add
for:indexonly if you display or store the position. - Move to
iteratorwhen the first or last item needs its own markup. - Precompute in JavaScript when the template needs a value it cannot calculate.
- Always keep the key on the first element in the loop, based on a stable Id.
Exercise
A list of order lines must show a "Subtotal" row only after the last line. The lines have a unique Id. Which directive fits best, and what key expression do you write on the first element?
Show answer
Use iterator, because it.last identifies the last line without extra JavaScript. Write key={it.value.Id} on the first element inside the iterator template, and wrap the Subtotal row in lwc:if={it.last}.
Exercise
A teammate's for:each list uses key={position} from for:index. Switching to iterator does not fix the flicker when items are reordered. Why?
Show answer
Both directives need a stable, unique key, and an index changes when items move. The bug is in the key, not the directive. Use the item's own Id, for example key={contact.Id}.