Sub topic 3
Using the current item and for:index
This chapter assumes you know basic HTML and JavaScript, including arrays. It also assumes you can build an LWC with an HTML template and a class that extends LightningElement. It covers how for:item names the current element and how for:index exposes its position.
The current item
for:each needs two things: the array and a name for the current element. The for:item attribute supplies that name. Inside the iteration template, you use the name like any other bound value.
<!-- contactList.html -->
<template>
<ul>
<template for:each={contacts} for:item="contact">
<li key={contact.Id}>{contact.Name}</li>
</template>
</ul>
</template>// contactList.js
import { LightningElement } from 'lwc';
export default class ContactList extends LightningElement {
contacts = [
{ Id: '003A', Name: 'Amy Taylor' },
{ Id: '003B', Name: 'Ravi Patel' },
{ Id: '003C', Name: 'Lena Fischer' }
];
}The name in for:item="contact" is yours to choose. It exists only inside the iteration template. You can read properties with dot notation, such as {contact.Name}. You cannot write expressions like {contact.Name.toUpperCase()}. Templates only bind values, so any calculation belongs in JavaScript. A later chapter covers that.
If the array holds plain strings or numbers, the item is the value itself. You would write {item} with no property access.
Reading the position with for:index
Add for:index to get the zero-based position of the current item. As with for:item, you choose the name.
<template>
<ul>
<template for:each={contacts} for:item="contact" for:index="position">
<li key={contact.Id} data-position={position}>{contact.Name}</li>
</template>
</ul>
</template>With the three contacts above, position is 0 for Amy Taylor, 1 for Ravi Patel and 2 for Lena Fischer.
The loop looks like this:
Showing a human-friendly number
The index starts at 0, so it is not suitable for display as a rank. The template cannot compute {position + 1}. If you need a one-based number, add it to each object in JavaScript. Chapter "Preparing display values in JavaScript" shows how.
Where the index is useful
- Writing it to a
data-*attribute so an event handler can find the item. - Debugging, to see which pass produced which element.
- Passing it to a child component that needs to know its position.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| Using `contact` outside the iteration template | The value is undefined, or compilation fails | Keep every use inside the `for:each` template |
| Writing an expression such as `{position + 1}` | Template compile error | Compute the value in a getter or in a mapped array |
| Omitting `for:item` | Compile error, because the item has no name | Always set `for:item` |
| Putting the key on the `<template>` tag | Key is ignored or rejected | Put `key` on the first element inside the template |
Exercise
A template uses for:each={contacts} for:item="contact" for:index="position". What is the value of position on the second pass, and why should you not use it as the key?
Show answer
It is 1, because the index is zero-based. It should not be the key because it identifies a place in the array, not the item. After a reorder or removal, the same index points to a different item, so the framework can reuse the wrong elements.