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": [
{
"id": 6789374705729,
"vendor": "Polina's Potent Potions"
},
{
"id": 6790277595201,
"vendor": "Ted's Apothecary Supply"
},
{
"id": 6792602320961,
"vendor": "Polina's Potent Potions"
},
{
"id": 6792607825985,
"vendor": "Clover's Apothecary"
},
{
"id": 6816899596353,
"vendor": "Polina's Potent Potions"
},
{
"id": 6790315212865,
"vendor": "Clover's Apothecary"
},
{
"id": 6792635580481,
"vendor": "Clover's Apothecary"
},
{
"id": 6823781007425,
"vendor": "Polina's Potent Potions"
},
{
"id": 6792632795201,
"vendor": "Ted's Apothecary Supply"
},
{
"id": 6792575352897,
"vendor": "Clover's Apothecary"
},
{
"id": 6786188247105,
"vendor": "Polina's Potent Potions"
},
{
"id": 6786989391937,
"vendor": "Polina's Potent Potions"
},
{
"id": 6792613462081,
"vendor": "Ted's Apothecary Supply"
},
{
"id": 6784387022913,
"vendor": "Polina's Potent Potions"
},
{
"id": 7067094777921,
"vendor": "Polina's Potent Potions"
},
{
"id": 6817196572737,
"vendor": "Polina's Potent Potions"
},
{
"id": 6821963563073,
"vendor": "Polina's Potent Potions"
},
{
"id": 6790469058625,
"vendor": "Ted's Apothecary Supply"
},
{
"id": 6792596455489,
"vendor": "Clover's Apothecary"
}
]
}
}Output
No index found
Output
No index foundWas this page helpful?