# each

The `each` function iterates through each item of an array, similar to a foreach loop.

The current item is accessible via the `$this` variable. The current index is accessible via `$key`.

The return values from `each` are ignored — it is useful when you want to modify values in-place.

Most commonly used with:

* [search](/functions/search.md)
* [recursive descent](/syntax/recursive-descent.md)
* `--root` flag

#### Syntax

```
<array>.each(expression)
```

#### Examples

**Modifying data in-place**

```bash
$ echo '[1, 2, 3]' | dasel -i json 'each($this = $this + 1)'
[
    2,
    3,
    4
]
```

**Uppercase all names in a file**

```bash
$ echo '["alice", "bob"]' | dasel -i json --root 'each($this = toUpper($this))'
[
    "ALICE",
    "BOB"
]
```

**Combined with search — update deeply nested values**

```bash
$ cat data.json | dasel -i json --root 'search(has("status")).each(status = "active")'
```

This finds all objects with a `status` field anywhere in the document and sets them to `"active"`.


---

# Agent Instructions: 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:

```
GET https://daseldocs.tomwright.me/functions/each.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
