Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Accessing indexed item in metafield list / array

Accessing indexed item in metafield list / array

LeanMassLabs
Shopify Partner
2 0 0

In many other languages, it's common to reach specific items by using an index to locate specific items in an array.

 

In this specific task, I'm trying to access an item in a metafield list.

Given the metafields returns an array - I would expect it to be possible to reach a specific item.

For example:

{{ product.metafields.shopify['color-pattern'].value }} // Returns an array 

 

Which allows for looping:

{% for colorPattern in product.metafields.shopify['color-pattern'].value %}
// colorPattern.color; #HEX_CODE
{% endfor %}

 

 

 

I'm already having a forloop active a few lines above, allowing me to access the specific index with forloop.index0. I wouldn't want to run an unnecessary, nested forloop.

However, this does not work as I would expect

 

{% assign availableColors = product.metafields.shopify['color-pattern'].value %}

<div
class="color-palette"
style="background-color: {{ availableColors[forloop.index0] }}" // returns NULL
>
...

 

 

 

Nor does any other (what I could spot) way of reaching the value with indexing, either.

Is is possible to access metafields' list items by indexing, or will I have to resort to nested forlooping?

Thanks.

Replies 0 (0)