find_ index
array | find_index: string, string
returns number
Returns the index of the first item in an array with a specific property value.
This requires you to provide both the property name and the associated value.
{% assign index = collection.products | find_index: 'vendor', "Polina's Potent Potions" %}
{{ index }}
{% assign index = collection.products | find_index: 'vendor', "Polina's Potent Potions" %}
{{ index }}
{
"collection": {
"products": [
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Ted's Apothecary Supply"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Clover's Apothecary"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Clover's Apothecary"
},
{
"vendor": "Clover's Apothecary"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Ted's Apothecary Supply"
},
{
"vendor": "Clover's Apothecary"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Ted's Apothecary Supply"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Ted's Apothecary Supply"
},
{
"vendor": "Clover's Apothecary"
}
]
}
}
Output
0
Anchor to Returns `nil` when no items match the specified property value.
Returns nil
when no items match the specified property value.
nil
when no items match the specified property value.{% assign index = collection.products | find_index: 'vendor', "Polina's Potions" %}
{{ index | default: "No index found" }}
{% assign index = collection.products | find_index: 'vendor', "Polina's Potions" %}
{{ index | default: "No index found" }}
{
"collection": {
"products": [
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Ted's Apothecary Supply"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Clover's Apothecary"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Clover's Apothecary"
},
{
"vendor": "Clover's Apothecary"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Ted's Apothecary Supply"
},
{
"vendor": "Clover's Apothecary"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Ted's Apothecary Supply"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Polina's Potent Potions"
},
{
"vendor": "Ted's Apothecary Supply"
},
{
"vendor": "Clover's Apothecary"
}
]
}
}
Output
No index found
Was this page helpful?