# Array filters

Array filters change the output of arrays.

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.

```liquid
{{ product.tags | join: ', ' }}
```

```text
tag1, tag2, tag3
```

## first

Returns the first element of an array.

```liquid
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags | first }}
```

```text
sale
```

You can use `first` with dot notation when you need it inside a [tag](https://developer.versacommerce.de/articles/liquid-tags).

```liquid
{% if product.tags.first == "sale" %}
  This product is on sale!
{% endif %}
```

## last

Returns the last element of an array.

```liquid
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags | last }}
```

```text
awesome
```

You can use `last` with dot notation when you need it inside a [tag](https://developer.versacommerce.de/articles/liquid-tags).

```liquid
{% if product.tags.last == "sale"%}
  This product is on sale!
{% endif %}
```

Using `last` on a string returns the last character of the string.

```liquid
<!-- product.title = "Awesome Shoes" -->
{{ product.title | last }}
```

```text
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`](#uniq) filter.

```liquid
{% assign fruits = "apples, oranges, peaches, tomatoes" | split: ", " %}
{% assign vegetables = "broccoli, carrots, lettuce, tomatoes" | split: ", " %}

{% assign plants = fruits | concat: vegetables %}

{{ plants | join: ", " }}
```

```text
apples, oranges, peaches, tomatoes, broccoli, carrots, lettuce, tomatoes
```

You can chain several `concat` filters to combine more than two arrays:

```liquid
{% 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: ", " }}
```

```text
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]`.

```liquid
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags[2] }}
```

```text
womens
```

## map

Takes an attribute of the array elements as parameter and creates an array of the values of that attribute.

```liquid
<!-- collection.title = "Spring", "Summer", "Fall", "Winter" -->
{% assign collection_titles = collections | map: 'title' %}
{{ collection_titles }}
```

```text
SpringSummerFallWinter
```

## reverse

Reverses the order of the elements in an array.

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

{{ my_array | reverse | join: ", " }}
```

```text
plums, peaches, oranges, apples
```

## size

Returns the size of a string (the number of characters) or of an array (the number of elements).

```liquid
{{ 'The quick brown fox jumps over a lazy dog.' | size }}
```

```text
42
```

You can use `size` with dot notation when you need it inside a [tag](https://developer.versacommerce.de/articles/liquid-tags).

```liquid
{% 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.

```liquid
{% assign products = collection.products | sort: 'price' %}
{% for product in products %}
  <h4>{{ product.title }}</h4>
{% endfor %}
```

The sort order is case-sensitive.

```liquid
 <!-- products = "a", "b", "A", "B" -->
{% assign products = collection.products | sort: 'title' %}
{% for product in products %}
   {{ product.title }}
{% endfor %}
```

```text
A B a b
```

## where

Creates an array that contains only the objects with a given property value, or, by default, any [truthy](https://developer.versacommerce.de/articles/liquid-basics-true-and-false#truthy) value.

```liquid
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 %}
```

```text
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](https://developer.versacommerce.de/articles/liquid-basics-types#boolean) or [truthy](https://developer.versacommerce.de/articles/liquid-basics-true-and-false#truthy), for example the [available](https://developer.versacommerce.de/articles/liquid-objects-product#product-available) property of products.

```liquid
{% 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.

```liquid
{% assign fruits = "orange apple banana apple orange" %}
{{ fruits | split: ' ' | uniq | join: ' ' }}
```

```text
orange apple banana
```
