Array Filters - aareano/ifshop-wiki GitHub Wiki
These filters can be applied to arrays, in addition to Shopify's.
array
Converts the value into an array. nil | array
will return an empty array.
collect
Invokes the filter on each element of the array.
Input
{% assign fruits = " apples, oranges , tomatoes" | split: ", " %}
{{ fruits | collect: "strip" | collect: "upcase" | join: ", " }}
{{ fruits | collect: "strip" | collect: "size" | join: ", " }}
Output
APPLES, ORANGES, TOMATOES
6, 7, 8
difference
Removes all elements from the first array that are in the second array.
Input
{% assign fruits = "apples, oranges, tomatoes" | split: ", " %}
{% assign vegetables = "broccoli, tomatoes" | split: ", " %}
{% assign strictly_fruits = fruits | difference: vegetables %}
{{ strictly_fruits | join: ", " }}
Output
apples, oranges
pop
Returns the last element of the array.
push
Adds an element to the end of an array.
symmetric_difference
Takes the symmetric difference (in one array, but not both) of two arrays.
Input
{% assign fruits = "apples, oranges, tomatoes" | split: ", " %}
{% assign vegetables = "broccoli, tomatoes" | split: ", " %}
{% assign plants = fruits | symmetric_difference: vegetables %}
{{ plants | join: ", " }}
Output
apples, oranges, broccoli
union
Returns an array of unique elements from the concatentation of two arrays.
{{ my_array | union: my_other_array }}
is the same as {{ my_array | concat: my_other_array | uniq }}
zip
Interleaves two arrays.
Input
{% assign a = "1, 2, 3" | split: ", " %}
{% assign b = "4, 5, 6" | split: ", " %}
{% assign c = "7, 8" | split: ", " %}
{{ a | zip: b | join: ", " }}
{{ a | zip: b | first | join: ", " }}
{{ a | zip: c | last | join: ", " }}
Output
1, 4, 2, 5, 3, 6
1, 4
3,