find
Returns the first map in a slice whose named field equals the given value, scanning in order.
Matching is exact on value and type, so a field holding the integer 42 is not matched by the string "42". When nothing matches it returns a non-fatal ErrAllowsDefaultFunc error so the default modifier can supply a fallback.
[]anystringanyanymap[string]anystringanyanyBehaviors
- Catchable miss
- When the data it looks for is not there, it reports a miss rather than failing the render, so
defaultcan supply a fallback and aniforforreads the absence as false.
Returns the first map in a slice whose key field equals the wanted value.
Given
{"items": [{"name": "Coffee", "status": "sold-out"}, {"name": "Tea", "status": "active"}]}Template
{{ items | find:'status','active' }}Result
{
"name": "Tea",
"status": "active"
}Pairs find with default, so a search that matches nothing falls back to a supplied value instead of failing the render.
Given
{"items": [{"name": "Coffee", "status": "sold-out"}]}Template
{{ items | find:'status','active' | default:'none in stock' }}Result
none in stockMatches on an exact value and type, so a numeric id is compared as a number rather than as its text form.
Given
{"users": [{"id": 1, "name": "Ada"}, {"id": 2, "name": "Bo"}]}Template
{{ users | find:'id',2 }}Result
{
"id": 2,
"name": "Bo"
}Returns the map itself when its key field equals the wanted value.
Given
{"item": {"name": "Tea", "status": "active"}}Template
{{ item | find:'status','active' }}Result
{
"name": "Tea",
"status": "active"
}Falls back to a supplied value when a single map's field does not match the wanted value, instead of failing the render.
Given
{"item": {"name": "Tea", "status": "brewing"}}Template
{{ item | find:'status','active' | default:'unavailable' }}Result
unavailable