Iteration (dealing with lists)
Looping over lists is a very frequent need and in this guide we'll go over the various ways you can do it.
Here are the essential aspects of iteration but you can learn more in the official documentation.
The simplest loop
Let's use the data we defined in an earlier section:
{
"orderId": 1234,
"orderStatus": "pending",
"invoiceId": null,
"client": {
"civility": "Mr",
"fullName": "Peter Parker",
"isNewClient": true
},
"lineItems": [
{ "product": "Super strong silk", "quantity": 100, "price": 123.45 },
{ "product": "Electronic components", "quantity": 12, "price": 12.34 }
]
}We can loop over the lineItems array to list our products using a for loop. It will repeat the code for each item in the array:
The resulting HTML will look like this:
Related links
The forloop object
Inside a for loop, you get access to a special object that stores the current state of the loop. It can be useful to build special cases for the first or last iteration, for instance.
Last updated
Was this helpful?
