Array filters modify arrays.

concat

array | concat: array

Concatenates (combines) two arrays.

📘

Note

The concat filter won't filter out duplicates. If you want to remove duplicates, then you need to use the uniq filter.

{% assign cold_climate_fruits = 'Apple,Pear' | split: ',' %}
{% assign warm_climate_fruits = 'Dragon fruit,Mango' | split: ',' %}
{% assign all_fruits = cold_climate_fruits | concat: warm_climate_fruits %}

{% for fruit in all_fruits %}
  {{ fruit }}
{% endfor %}
Apple
Pear
Dragon fruit
Mango

first

array | first

Returns the first item in an array.

{% assign first_product = collection.products | first %}

{{ first_product.title }}
{
  "collection": {
    "products": [
      {
        "title": "Fit 3 Denim Shirt"
      },
      {
        "title": "Ace Cashmere Beanie"
      }
    ]
  }
}
Fit 3 Denim Shirt

join

array | join
returns  string

Combines all of the items in an array into a single string, separated by a space.

{{ collection.tags | join }}
{
  "collection": {
    "tags": [
      "cold",
      "fresh",
      "large"
    ]
  }
}
cold fresh large

Custom separator

array | join: string

You can specify a custom separator for the joined items.

{{ collection.tags | join: ', ' }}
{
  "collection": {
    "tags": [
      "cold",
      "fresh",
      "large"
    ]
  }
}
cold, fresh, large

last

array | last

Returns the last item in an array.

{% assign last_product = collection.products | last %}

{{ last_product.title }}
{
  "collection": {
    "products": [
      {
        "title": "Fit 3 Denim Shirt"
      },
      {
        "title": "Ace Cashmere Beanie"
      }
    ]
  }
}
Ace Cashmere Beanie

map

array | map: string

Creates an array of values from a specific property of the items in an array.

{% assign product_titles = collection.products | map: 'title' %}

{{ product_titles | join: '@' }}
{
  "collection": {
    "products": [
      {
        "title": "Fit 3 Denim Shirt"
      },
      {
        "title": "Ace Cashmere Beanie"
      }
    ]
  }
}
Fit 3 Denim Shirt@Ace Cashmere Beanie

reverse

array | reverse

Reverses the order of the items in an array.

Original order:
{{ collection.products | map: 'title' | join: '@' }}

Reverse order:
{{ collection.products | reverse | map: 'title' | join: '@' }}
{
  "collection": {
    "products": [
      {
        "title": "Fit 3 Denim Shirt"
      },
      {
        "title": "Ace Cashmere Beanie"
      }
    ]
  }
}
Original order:
Fit 3 Denim Shirt@Ace Cashmere Beanie

Reverse order:
Ace Cashmere Beanie@Fit 3 Denim Shirt

size

variable | size
returns  number

Returns the size of a string or array.

The size of a string is the number of characters that the string includes. The size of an array is the number of items in the array.

{{ collection.title | size }}
{{ collection.products | size }}
17
2

sort

array | sort

Sorts the items in an array in case-sensitive alphabetical, or numerical, order.

{% assign tags = collection.tags | sort %}

{% for tag in tags %}
   {{ tag }}
{% endfor %}
{
  "collection": {
    "tags": [
      "Burning",
      "fresh",
      "music",
      "plant",
      "Salty"
    ]
  }
}

Burning
Salty
fresh
music
plant

Sort by an array item property

array | sort: string

You can specify an array item property to sort the array items by. You can sort by any property of the object that you're sorting.

{% assign products = collection.products | sort: 'price' %}

{% for product in products %}
  {{ product.title }}
{% endfor %}
{
  "collection": {
    "products": [
      {
        "title": "Fit 3 Denim Shirt",
        "price": "22.00"
      },
      {
        "title": "Ace Cashmere Beanie",
        "price": "12.88"
      }
    ]
  }
}
Ace Cashmere Beanie
Fit 3 Denim Shirt

uniq

array | uniq

Removes any duplicate items in an array.

{% assign random_number = '2,2,3,3,4' | split: ',' %}

{{ random_number | uniq | join: ',' }}
2,3,4