Array filters change the output of arrays.
join
Joins the elements of an array with the character passed as parameter. The result is a single string.
{{ product.tags | join: ', ' }}
tag1, tag2, tag3
first
Returns the first element of an array.
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags | first }}
sale
You can use first with dot notation when you need it inside a tag.
{% if product.tags.first == "sale" %}
This product is on sale!
{% endif %}
last
Returns the last element of an array.
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags | last }}
awesome
You can use last with dot notation when you need it inside a tag.
{% if product.tags.last == "sale"%}
This product is on sale!
{% endif %}
Using last on a string returns the last character of the string.
<!-- product.title = "Awesome Shoes" -->
{{ product.title | last }}
s
concat
Concatenates (combines) an array with another array. The resulting array contains all elements of the original arrays. concat does not remove duplicates unless you also use the uniq filter.
{% assign fruits = "apples, oranges, peaches, tomatoes" | split: ", " %}
{% assign vegetables = "broccoli, carrots, lettuce, tomatoes" | split: ", " %}
{% assign plants = fruits | concat: vegetables %}
{{ plants | join: ", " }}
apples, oranges, peaches, tomatoes, broccoli, carrots, lettuce, tomatoes
You can chain several concat filters to combine more than two arrays:
{% assign fruits = "apples, oranges, peaches" | split: ", " %}
{% assign vegetables = "broccoli, carrots, lettuce" | split: ", " %}
{% assign animals = "dogs, cats, birds" | split: ", " %}
{% assign things = fruits | concat: vegetables | concat: animals %}
{{ things | join: ", " }}
apples, oranges, peaches, broccoli, carrots, lettuce, dogs, cats, birds
index
Returns the element at the given index position in an array. Array numbering starts at zero, so the first element is referenced with [0].
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags[2] }}
womens
map
Takes an attribute of the array elements as parameter and creates an array of the values of that attribute.
<!-- collection.title = "Spring", "Summer", "Fall", "Winter" -->
{% assign collection_titles = collections | map: 'title' %}
{{ collection_titles }}
SpringSummerFallWinter
reverse
Reverses the order of the elements in an array.
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array | reverse | join: ", " }}
plums, peaches, oranges, apples
size
Returns the size of a string (the number of characters) or of an array (the number of elements).
{{ 'The quick brown fox jumps over a lazy dog.' | size }}
42
You can use size with dot notation when you need it inside a tag.
{% if collections.frontpage.products.size > 10 %}
There are more than 10 products in this collection!
{% endif %}
sort
Sorts the elements of an array by a given attribute of the elements.
{% assign products = collection.products | sort: 'price' %}
{% for product in products %}
<h4>{{ product.title }}</h4>
{% endfor %}
The sort order is case-sensitive.
<!-- products = "a", "b", "A", "B" -->
{% assign products = collection.products | sort: 'title' %}
{% for product in products %}
{{ product.title }}
{% endfor %}
A B a b
where
Creates an array that contains only the objects with a given property value, or, by default, any truthy value.
All products:
{% for product in collection.products %}
- {{ product.title }}
{% endfor %}
{% assign kitchen_products = collection.products | where: "type", "kitchen" %}
Kitchen products:
{% for product in kitchen_products %}
- {{ product.title }}
{% endfor %}
All products:
- Vacuum
- Spatula
- Television
- Garlic press
Kitchen products:
- Spatula
- Garlic press
You can use where with a property name and no target value if the property is Boolean or truthy, for example the available property of products.
{% assign available_products = collection.products | where: "available" %}
Available products:
{% for product in available_products %}
- {{ product.title }}
{% endfor %}
uniq
Removes all duplicate elements from an array.
{% assign fruits = "orange apple banana apple orange" %}
{{ fruits | split: ' ' | uniq | join: ' ' }}
orange apple banana