pluck
Reads one named field from every element of a slice of maps and returns the collected values as a slice, in order.
The result length always matches the input length, so a field that is absent from an element is never skipped or padded over. An empty slice yields an empty slice.
A field missing from an element, or an element holding nothing to read the
field from, is a miss, so | pluck:'key' | default:[] falls back to an empty
slice rather than failing. An element that is not a map at all is a terminal
error, since plucking a field from a number is a template that cannot mean
anything.
[]anystring[]anyBehaviors
- 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.
Reads one named field from every element of a slice of maps and returns the collected values in order.
Given
{"users": [{"name": "Alice", "role": "admin"}, {"name": "Bob", "role": "viewer"}]}Template
{{ users | pluck:'name' }}Result
[
"Alice",
"Bob"
]Collects a field and then reads the first value out of the result, chaining two access modifiers in one pipeline.
Given
{"users": [{"name": "Alice"}, {"name": "Bob"}]}Template
{{ users | pluck:'name' | first }}Result
AliceReturns an empty slice when there is nothing to read from, so the result length always tracks the input.
Given
{"users": []}Template
{{ users | pluck:'name' }}Result
[]Collects a numeric field from every element, keeping the values as numbers.
Given
{"orders": [{"item": "Coffee", "price": 3}, {"item": "Cake", "price": 5}]}Template
{{ orders | pluck:'price' }}Result
[
3,
5
]