I’m experiencing some problems working with metafield of type Metaobject (List).
The functionality isn’t well documented and usual way of working with arrays doesn’t work either.
For example, trying to access a particular metaobject from metaobject list by using square brackets and an index won’t work (metaobject_list[0]).
But iterating through it using for in loop works fine.
Is there a way to access the first metaobject in the list through index? Can somebody direct me to the relavant information in the docs, because I couldn’t find anything myself?
you can use first
and last
to get the first and last element of an array (docs)
I’m aware of that. As I said the problem with it, is that it doesn’t behave as a normal array.
{{ product.metafields.shopify.jewelry-material.value.first.label | json }}
This code is gonna return null.
While this
{% for material in product.metafields.shopify.jewelry-material.value %}
{{ material.label | json }}
{% endfor %}
returns the desired value.
apologies i missed that – no you cannot access array elements through index.
Store it in a JavaScript variable it keeps the same order and you can pick an item from it