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 }}
Code
{% assign index = collection.products | find_index: 'vendor', "Polina's Potent Potions" %}
{{ index }}Data
{
"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
Output
0Anchor 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" }}
Code
{% assign index = collection.products | find_index: 'vendor', "Polina's Potions" %}
{{ index | default: "No index found" }}Data
{
"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
Output
No index foundWas this page helpful?