# 数组过滤器

Array filters 用于修改[数组](/zh-CN/docs/theme/references/liquid/basics/liquid-types#array)。

## concat

`array | concat: array`

将两个数组拼接（合并）在一起。

**备注**

`concat` filter 不会过滤重复项。如果需要删除重复项，请使用 [uniq filter](/zh-CN/docs/theme/references/liquid/filters/array-filters#uniq)。

**代码**

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

**输出**

```html
Apple
Pear
Dragon fruit
Mango
```

## first

`array | first`

返回数组中的第一个元素。

**代码**

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

{{ first_product.title }}
```

**数据**

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

**输出**

```html
Fit 3 Denim Shirt
```

## join

`array | join`

returns  [string](../basics/liquid-types#string)

将数组中的所有元素合并为单个[字符串](/zh-CN/docs/theme/references/liquid/basics/liquid-types#string)，以空格分隔。

**代码**

```liquid
{{ collection.tags | join }}
```

**数据**

```json
{
  "collection": {
    "tags": [
      "cold",
      "fresh",
      "large"
    ]
  }
}
```

**输出**

```html
cold fresh large
```

### 自定义分隔符

`array | join: string`

您可以为合并的元素指定自定义分隔符。

**代码**

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

**数据**

```json
{
  "collection": {
    "tags": [
      "cold",
      "fresh",
      "large"
    ]
  }
}
```

**输出**

```html
cold, fresh, large
```

## last

`array | last`

返回数组中的最后一个元素。

**代码**

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

{{ last_product.title }}
```

**数据**

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

**输出**

```html
Ace Cashmere Beanie
```

## map

`array | map: string`

从数组中各元素的特定属性创建一个新数组。

**代码**

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

{{ product_titles | join: '@' }}
```

**数据**

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

**输出**

```html
Fit 3 Denim Shirt@Ace Cashmere Beanie
```

## reverse

`array | reverse`

反转数组中元素的顺序。

**代码**

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

Reverse order:
{{ collection.products | reverse | map: 'title' | join: '@' }}
```

**数据**

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

**输出**

```html
Original order:
Fit 3 Denim Shirt@Ace Cashmere Beanie

Reverse order:
Ace Cashmere Beanie@Fit 3 Denim Shirt
```

## size

`variable | size`

returns  [number](../basics/liquid-types#number)

返回字符串或数组的大小。

字符串的大小为其包含的字符数，数组的大小为其元素个数。

**代码**

```liquid
{{ collection.title | size }}
{{ collection.products | size }}
```

**输出**

```html
17
2
```

## sort

`array | sort`

按区分大小写的字母顺序或数值顺序对数组中的元素进行排序。

**代码**

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

{% for tag in tags %}
   {{ tag }}
{% endfor %}
```

**数据**

```json
{
  "collection": {
    "tags": [
      "Burning",
      "fresh",
      "music",
      "plant",
      "Salty"
    ]
  }
}

```

**输出**

```html
Burning
Salty
fresh
music
plant
```

### 按数组元素属性排序

`array | sort: string`

您可以指定数组元素的某个属性作为排序依据，可以按正在排序的 object 的任意属性进行排序。

**代码**

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

{% for product in products %}
  {{ product.title }}
{% endfor %}
```

**数据**

```json
{
  "collection": {
    "products": [
      {
        "title": "Fit 3 Denim Shirt",
        "price": "22.00"
      },
      {
        "title": "Ace Cashmere Beanie",
        "price": "12.88"
      }
    ]
  }
}
```

**输出**

```html
Ace Cashmere Beanie
Fit 3 Denim Shirt
```

## uniq

`array | uniq`

删除数组中的重复元素。

**代码**

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

{{ random_number | uniq | join: ',' }}
```

**输出**

```html
2,3,4
```
