filter
Returns the items of a slice whose named field equals the given value.
Each item is looked up by key, with dot notation reaching into a nested map (for example "meta.published"). Numbers compare by value across the int and float kinds, so 10 matches 10.0. An item is dropped when the field is missing or the values differ, and a slice where nothing matches comes back empty rather than as an error.
InputArgumentsReturns
Source[]anystringany[]anyBasicSource
Returns the items of a slice whose named field equals the search value.
Given
{"items": [{"name": "Alice", "role": "admin"}, {"name": "Bob", "role": "viewer"}, {"name": "Carol", "role": "admin"}]}Template
{{ items | filter:'role','admin' }}Result
[
{
"name": "Alice",
"role": "admin"
},
{
"name": "Carol",
"role": "admin"
}
]NestedSource
Filters on a nested field reached with dot notation.
Given
{"posts": [{"title": "Draft", "meta": {"published": false}}, {"title": "Live", "meta": {"published": true}}]}Template
{{ posts | filter:'meta.published',true }}Result
[
{
"meta": {
"published": true
},
"title": "Live"
}
]No matchSource
Returns an empty slice when nothing matches rather than an error.
Given
{"items": [{"name": "Alice", "role": "admin"}, {"name": "Bob", "role": "viewer"}]}Template
{{ items | filter:'role','owner' }}Result
nullNumberSource
Matches numbers by value, so an integer field equals a whole-number search.
Given
{"items": [{"name": "A", "score": 10}, {"name": "B", "score": 20}]}Template
{{ items | filter:'score',10 }}Result
[
{
"name": "A",
"score": 10
}
]