Array Filters

Array filters provide means to perform common and useful operations on arrays.


compact

Removes falsy (false, null, empty) values from the array.

Input

 {% assign my_bugs = "ants, bees, ," | split: ", " | compact %}
 {{ my_bugs | join: ", " }}

Output

 ants, bees

concat

Combines an array with another array. The resulting array will contain all the elements from both arrays. Duplicates will not be removed (that can be done with uniq filter).

Input

 {% assign my_bugs = "ants, bees, spiders" | split: ", " %}
 {% assign my_other_bugs = "wasps" | split: ", " %}
 {% assign all_bugs = my_bugs | concat: my_other_bugs %}

 {{ all_bugs | join: ", " }}

Output

 ants, bees, spiders, wasps

find

Returns the value of first matching key

Input

 All products:
 {% for product in products %}
 - {{ product.title }}
 {% endfor %}

 First match:
 {{ first_product = products | find: "type", "kitchen" }}

Output

 All products:
 - Vacuum
 - Spatula
 - Television
 - Garlic press

 First match:
 - Spatula

first

Returns the first element of an array.

Input

 {{ "The quick brown fox jumps over the lazy dog" | split: " " | first }}

Output

 The

join

Joins elements of an array with a given character between them.

Input

 {{ "The, quick, brown, fox, jumps, over, the, lazy, dog" | split ", " | join " " }}

Output

 The quick brown fox jumps over the lazy dog

lst

Returns the last element of an array.

Input

 {{ "The quick brown fox jumps over the lazy dog" | split: " " | last }}

Output

 dog

map

Collect a given property from and array of objects

Input

 {% assign product_names = category.products | map: 'name' %}

 {% for name in product_names %}
 * {{ name }}
 {% endfor %}

Output

 * Skateboard
 * Beenie
 * Jeans

random

Return a random element from the array.

Input

 {% assign my_bugs = "ants, bees, flies" | split: ", " %}
 {{ my_bugs | random }}
 {{ my_bugs | random }}
 {{ my_bugs | random }}

Output

 bees
 flies
 bees

reverse

Reverses the order of the items in an array. reverse cannot reverse a string.

Input

 {% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}

Output

 plums, peaches, oranges, apples

size

Returns the number of characters in a string or the number of items in an array.

Input

 {{ "The quick brown fox jumps over the lazy dog" | size }}
 {{ "The quick brown fox jumps over the lazy dog" | split: " " | size }}

Output

 43
 9

slice

Returns a substring of one character or series of array items beginning at the index specified by the first argument. An optional second argument specifies the length of the substring or number of array items to be returned.

Input

 {{ "Liquid" | slice: 0 }}
 {{ "Liquid" | slice: 2, 5 }}
 {{ "The quick brown fox jumps over the lazy dog" | split: " " | slice: 1,2 | join: " " }}

Output

 L
 quid
 quick brown

sort

Sort the elements of an array. An optional argument specifies which property of the array’s items to use for sorting.

Input

 {% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}
 {{ my_array | sort | join: ", " }}

Output

 Sally Snake, giraffe, octopus, zebra

uniq

Remove duplicate elements from an array.

Input

 {% assign my_array = "ants, bugs, bees, bugs, ants" | split: ", " %}
 {{ my_array | uniq | join: ", " }}

Output

 ants, bugs, bees

where

Creates an array including only the objects with a given property value, or any truthy value by default.

In this example, assume you have a list of products and you want to show your kitchen products separately. Using where, you can create an array containing only the products that have a “type” of “kitchen”.

Input

 All products:
 {% for product in products %}
 - {{ product.title }}
 {% endfor %}

 {% assign kitchen_products = products | where: "type", "kitchen" %}

 Kitchen products:

 {% for product in kitchen_products %}
 - {{ product.title }}
 {% endfor %}

Output

 All products:
 - Vacuum
 - Spatula
 - Television
 - Garlic press

 Kitchen products:
 - Spatula
 - Garlic press