appending arrays

Highlighted
Shopify Partner
20 0 0

Is there no way to add elements to an existing array?

0 Likes
Astronaut
1925 0 368

To my knowledge, there is no way to directly add an element to an array in Liquid.

As a work-around for an array of strings, I join the array into a single string, append the element(s) to the string, and then split the string back into an array.

{% assign delim = "|" %}
{% assign existing_array = product.images %}
{% assign added_element = "image.jpg" %}
{% capture appended_array %}{{ existing_array | join: delim }}{{ delim }}{{ added_element }}{% endcapture %}
{% assign appended_array = appended_array | split: delim %}

I hope this helps.

0 Likes
Shopify Expert
9881 68 1420

There's an undocumented (aka: use with caution) filter.

{% assign arrayOne = 'a,b,c' | split:',' %}
{% assign arrayTwo = 'd,e,f' | split:',' %}

{% assign foo = arrayOne | concat:arrayTwo %}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Shopify Partner
20 0 0

Thanks guys! Trying it out this am.

0 Likes