# filter

The `filter` function filters the contents of an array, returning only elements that match the predicate. It works in a similar way to [JavaScript's Array.prototype.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter).

#### Syntax

```
<array>.filter(predicate)
```

#### Arguments

* **predicate** - A boolean expression evaluated against each element. Use `$this` to refer to the current element and `$key` to refer to the current index.

#### Examples

**Filter numbers**

```
[1, 2, 3, 4, 5].filter($this > 3)
// [4, 5]
```

**Filter objects by a field**

```
[
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 17},
    {"name": "Charlie", "age": 30}
].filter(age >= 18)
// [{"name": "Alice", "age": 25}, {"name": "Charlie", "age": 30}]
```

**CLI usage — find active users**

```bash
$ cat users.json | dasel -i json 'users.filter(active == true).map(name)'
```

**Filter by index using `$key`**

```
[10, 20, 30, 40, 50].filter($key >= 2)
// [30, 40, 50]
```

**Chained with other functions**

```
[1, 2, 3, 4, 5].filter($this > 2).first()
// 3
```


---

# 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/filter.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.
