In Liquid (Shopify) how am I able to get the index position of a specific array object?

Viewed 5010

In a snippet called 'sb' I have this content

{% assign seller_id = 'another_seller_shop_name,test_seller' | split: ',' %} 
{% assign seller_html = 'Another Seller Desc,Seller Description' | split: ',' %}

In the template page - in this case, collection-list I have referenced this snippet

{% include 'sb' %}{% assign seller_id_page = collection.title | replace: ' ','_' | downcase %}

'seller_id_page' will equal to one of the values in 'seller_id'. I just want to be able to return the position of this value, so I can then assign seller_html[x] an index value and render field correctly.

1 Answers

You will need to loop the array and get the index of the corresponding equality.

In code:

{% for item in seller_id %}
  {% if item == seller_id_page %}
    {% assign position = forloop.index0 %}
    {% break %} 
  {% endif %}
{% endfor %}

{{ seller_html[position] }}

That's the just of it.

Related