cbacelar
cbacelar

Reputation: 553

How to get an item of array by a field value?

I have an object called "template.Highlights" where exists 7 items. All items have 2 fields: 'name','desc'. How can I access (without multiples if's) an item through the value of the 'name' field? (the names are: 'hl1', 'hl2', 'hl3', 'hl4', 'hl5', 'hl6', 'hl7').

Upvotes: 0

Views: 42

Answers (1)

ferdynator
ferdynator

Reputation: 6410

{% for template.Highlights as highlight %}
    {% if highlight.name == 'hl5' %}
        {% set item = highlight %}
    {% endif %}
{% endfor %}
{{ item.name }}, {{ item.desc }}

Upvotes: 1

Related Questions