> For the complete documentation index, see [llms.txt](https://docs.cloud.hoverfly.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cloud.hoverfly.io/create-simulations/using-templating-in-simulations/looping-over-arrays.md).

# Looping over arrays

The ability to loop over arrays of data can be very useful in constructing a response. Some of the arrays typically in a response are:

* the elements of path
* the headers in the request
* an array of query parameters if there are more than one with the same name
* a JSON array extracted from the body of a request

The following block is used to loop over an array. If there are no items then the else block will execute:

* `{{#each}} {{else}} {{/each}}`

The following block is used to return the first item from an array. If there are no items then the else block will execute:

* `{{#first}} {{else}} {{/first}}`

You can use the `@index`, `@key` and `this` keywords to determine the index, key and value of the object under the iterator. You can use `@last` keyword to determine if the current object is the last one in the array.

### Example 1:

```handlebars
{{#each Request.Header}}
	{{@index}} : {{@key}} : {{this}}
{{else}}
	No Request Headers were provided
{{/each}}
```

### Example 2:

Using looping and conditional logic you can build our a JSON (or XML) response:

Given the following request body

```json
{
    "people": [
        {
            "name": "John",
            "surname": "Doe",
            "age": 30
        },
        {
            "name": "Jane",
            "surname": "Lee",
            "age": 29
        }
    ]
}
```

And creating the following simulation body template

```json
{ "ages":
    [
        {{#each (Request.Body 'JSONpath' '$.people') }}
        {{this.age}}
        {{#unless @last}},{{/unless}}
        {{/each}}
    ]
}
```

The result generated from the template and sent back in the body would be this:

```json
{
    "ages": [
        30,
        29
    ]
}
```

Follow a tutorial [here ](https://docs.cloud.hoverfly.io/create-simulations/using-templating-in-simulations/pages/oqKMVrUTaD0DLKTqOi7U#part-4.-iterating-over-request-collections)to learn more about looping


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cloud.hoverfly.io/create-simulations/using-templating-in-simulations/looping-over-arrays.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
