Sub topic 6
Detecting the First and Last Item
This chapter assumes you can write an LWC template and class, bind properties with curly braces, use lwc:if, and render an array with for:each and a key. Here you will use the iterator directive to mark the first and last items of a list without extra JavaScript.
Why `for:each` is not enough
The for:each directive gives you the current item and, with for:index, its position. It does not tell you whether the item is the first or the last one. Templates cannot run expressions such as index === contacts.length - 1, so you cannot test for the last item inside the template.
The iterator directive solves this. On each pass it gives you one object with four properties.
| Property | Meaning |
|---|---|
| `value` | The current item from the array |
| `index` | The zero-based position of the item |
| `first` | `true` only for the first item |
| `last` | `true` only for the last item |
Using the iterator directive
You name the iterator in the directive itself. In iterator:it={contacts}, the name it is a variable you choose. You then read its properties as it.value, it.index, it.first and it.last.
The component class holds a plain array of objects, and each object has a unique Id.
// 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 template marks the first and last items.
<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>With the three contacts above, the list reads:
- First: Amy Taylor
- Ravi Patel
- Lena Fischer (last)
Three details matter here:
- The
keygoes on the first element inside the iteration template, as withfor:each. Here it is<li>. - The key comes from the item, so it is
it.value.Id, notit.Id. it.firstandit.lastare booleans, so they work directly inlwc:if.
A list with one item has both first and last set to true. Both spans render for that item. Plan your markup for that case.
How the pieces connect
A practical use: separators and styling
The most common reason to check last is to avoid a trailing separator. Suppose you want names joined by commas. With for:each you would end up with a comma after the final name. With iterator, you show the comma only when the item is not last.
<template>
<p>
<template iterator:it={contacts}>
<span key={it.value.Id}>
{it.value.Name}<span lwc:if={it.last}></span><span lwc:else>, </span>
</span>
</template>
</p>
</template>Use first the same way, for example to add a heading style or to skip a top border on the first row.
Common errors
- Writing
it.Idinstead ofit.value.Id. The item is insidevalue. The template renders blanks or the key is undefined. - Putting
keyon the<template>tag. The key belongs on the first real element inside it. - Using
firstorlastwithfor:each. Those properties only exist on the iterator object. Switch toiteratorwhen you need them. - Forgetting the single-item case. One item is both first and last.
Exercise
A list uses iterator:row={rows} and must show a horizontal rule after every item except the last. Write the line that renders the rule, and state where key goes.
Show answer
Inside the iteration, after the item content, add <hr lwc:if={row.last}> is wrong because it shows the rule only for the last item. The correct test needs the opposite, so use <hr lwc:else> paired with an lwc:if={row.last} element, for example <span lwc:if={row.last}></span><hr lwc:else>. The key goes on the first element inside the iteration template, set to row.value.Id.