How to obtain a random value from a Liquid Array?

Hey,

Wodnering if anyone knows how to get a random value from a shopify array?

I have tried “| shuffle” but that doesnt seem to work.

Cheers,

Chris

1 Like

Hello @Chris_Sydney

{% assign myArray = "item1, item2, item2, item2, item2" | split: ", " %}

{% comment %} Generate a random index using JavaScript and store it in a Liquid variable {% endcomment %}
{% assign randomIndex = "" %}

{% comment %} array using the random index {% endcomment %}
{% for item in myArray %}
{% if forloop.index0 == randomIndex %}
{{ item }}
{% endif %}
{% endfor %}

Hey ZestardTech,

Awesome, thanks… I was trying to create a new a new value with | random, but looks like i need to use JS.

Cheers,

Chris

1 Like

Hello @Chris_Sydney

Working fine or not ?

Doesnt seem to work fo rme unless I need to be doing something more than copying and pasting..