Filter-Array(数组)
Filter-Array
Array(数组)
- concat
 - find
 - find_index
 - first
 - second
 - has
 - join
 - last
 - map
 - reject
 - reverse
 - size
 - sort
 - sort_natural
 - sum
 - uniq
 - where
 - array_append
 
concat
连接(组合)两个数组
- 过滤器concat不会过滤掉重复项。如果您想删除重复项,则需要使用过滤器uniq。
 
// 1.数据
{
  "collection": {
    "all_types": [
      "",
      "Animals & Pet Supplies",
      "Baking Flavors & Extracts",
      "Container",
      "Cooking & Baking Ingredients",
      "Dried Flowers",
      "Fruits & Vegetables",
      "Gift Cards",
      "Health",
      "Health & Beauty",
      "Invisibility",
      "Love",
      "Music & Sound Recordings",
      "Seasonings & Spices",
      "Water"
    ],
    "all_vendors": [
      "Clover's Apothecary",
      "Polina's Potent Potions",
      "Ted's Apothecary Supply"
    ]
  }
}
// 2.代码
{%- assign types_and_vendors = collection.all_types | concat: collection.all_vendors -%}
Types and vendors:
{% for item in types_and_vendors -%}
  {%- if item != blank -%}
    - {{ item }}
  {%- endif -%}
{%- endfor %}
// 3.输出
Types and vendors:
- Animals & Pet Supplies
- Baking Flavors & Extracts
- Container
- Cooking & Baking Ingredients
- Dried Flowers
- Fruits & Vegetables
- Gift Cards
- Health
- Health & Beauty
- Invisibility
- Love
- Music & Sound Recordings
- Seasonings & Spices
- Water
- Clover's Apothecary
- Polina's Potent Potions
- Ted's Apothecary Supply
find
返回具有特定属性值的数组中的第一个项目。
- 这要求您提供属性名称和关联值。
 
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Blue Mountain Flower",
        "vendor": "Polina's Potent Potions"
      },
      {
        "title": "Charcoal",
        "vendor": "Ted's Apothecary Supply"
      },
      {
        "title": "Crocodile tears",
        "vendor": "Polina's Potent Potions"
      },
      {
        "title": "Dandelion milk",
        "vendor": "Clover's Apothecary"
      },
      {
        "title": "Draught of Immortality",
        "vendor": "Polina's Potent Potions"
      }
    ]
  }
}
// 2.代码(查找子项: vendor的值为:"Polina's Potent Potions",则返回这个子项)
{% assign product = collection.products | find: 'vendor', "Polina's Potent Potions" %}
{{ product.title }}
// 3.输出
Blue Mountain Flower
find_index
返回具有特定属性值的数组中第一个项目的索引。
- 这要求您提供属性名称和关联值。
 
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Blue Mountain Flower",
        "vendor": "Polina's Potent Potions"
      },
      {
        "title": "Charcoal",
        "vendor": "Ted's Apothecary Supply"
      },
      {
        "title": "Crocodile tears",
        "vendor": "Polina's Potent Potions"
      }
    ]
  }
}
// 2.代码(查找子项: vendor的值为:"Polina's Potent Potions",则返回这个子项)
{% assign index = collection.products | find_index: 'vendor', "Polina's Potent Potions" %}
{{ index }}
// 3.输出(第一个子项的key是0)
0
first
返回数组中的第一个项目。
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Blue Mountain Flower",
        "vendor": "Polina's Potent Potions"
      },
      {
        "title": "Charcoal",
        "vendor": "Ted's Apothecary Supply"
      },
      {
        "title": "Crocodile tears",
        "vendor": "Polina's Potent Potions"
      }
    ]
  }
}
// 2.代码(查找子项: vendor的值为:"Polina's Potent Potions",则返回这个子项)
{%- assign first_product = collection.products | first -%}
{{ first_product.title }}
// 当您需要在标签或对象输出中使用它时,可以使用带有点符号的过滤器。
{{ collection.products.first.title }}
// 3.输出
Blue Mountain Flower
Blue Mountain Flower
second
返回数组中的第2个项目。
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Blue Mountain Flower",
        "vendor": "Polina's Potent Potions"
      },
      {
        "title": "Charcoal",
        "vendor": "Ted's Apothecary Supply"
      },
      {
        "title": "Crocodile tears",
        "vendor": "Polina's Potent Potions"
      }
    ]
  }
}
// 2.代码(查找子项: vendor的值为:"Polina's Potent Potions",则返回这个子项)
{%- assign second_product = collection.products | second -%}
{{ second_product.title }}
// 3.输出
Ted's Apothecary Supply
has
测试数组中的任何项目是否具有特定的属性值。
- 这要求您提供属性名称和关联值。
 
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Blue Mountain Flower",
        "vendor": "Polina's Potent Potions"
      },
      {
        "title": "Charcoal",
        "vendor": "Ted's Apothecary Supply"
      },
      {
        "title": "Crocodile tears",
        "vendor": "Polina's Potent Potions"
      }
    ]
  }
}
// 2.代码(查找子项: vendor的值为:"Polina's Potent Potions",则返回这个子项)
{% assign has_potent_potions = collection.products | has: 'vendor', "Polina's Potent Potions" %}
{{ has_potent_potions }}
// 3.输出 (存在则返回true,不存在则返回false)
true
join
将数组中的所有项目合并为一个字符串,并以空格分隔。
- 自定义分隔符:您可以为连接的项目指定自定义分隔符。
 
// 1.数据
{
  "collection": {
    "all_tags": [
      "extra-potent",
      "fresh",
      "healing",
      "ingredients"
    ]
  }
}
// 2.代码
{{ collection.all_tags | join }}
{{ collection.all_tags | join: ', ' }}
// 3.输出
extra-potent fresh healing ingredients
extra-potent, fresh, healing, ingredients
last
返回数组中的最后一项。
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Blue Mountain Flower"
      },
      {
        "title": "Charcoal"
      }
    ]
  }
}
// 2.代码
{%- assign last_product = collection.products | last -%}
{{ last_product.title }}
// 3.输出
Whole bloodroot
map
根据数组中项目的特定属性创建一个值数组。
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Draught of Immortality"
      },
      {
        "title": "Glacier ice"
      },
      {
        "title": "Health potion"
      },
      {
        "title": "Invisibility potion"
      }
    ]
  }
}
// 2.代码
{%- assign product_titles = collection.products | map: 'title' -%}
{{ product_titles | join: ', ' }}
// 3.输出
Draught of Immortality, Glacier ice, Health potion, Invisibility potion
reject
过滤数组以排除具有特定属性值的项目。
- 这要求您提供属性名称和关联值。
 
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Blue Mountain Flower",
        "vendor": "Polina's Potent Potions"
      },
      {
        "title": "Charcoal",
        "vendor": "Ted's Apothecary Supply"
      },
      {
        "title": "Crocodile tears",
        "vendor": "Polina's Potent Potions"
      }
    ]
  }
}
// 2.代码
{% assign polina_products = collection.products | reject: 'vendor', "Polina's Potent Potions" %}
Products from other vendors than Polina's Potent Potions:
{% for product in polina_products -%}
  - {{ product.title }}
{%- endfor %}
// 3.输出 (存在则返回true,不存在则返回false)
Products from other vendors than Polina's Potent Potions:
- Charcoal
reverse
反转数组中项目的顺序。
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Draught of Immortality"
      },
      {
        "title": "Glacier ice"
      },
      {
        "title": "Health potion"
      },
      {
        "title": "Invisibility potion"
      }
    ]
  }
}
// 2.代码
Original order:
{{ collection.products | map: 'title' | join: ', ' }}
Reverse order:
{{ collection.products | reverse | map: 'title' | join: ', ' }}
// 3.输出
Original order:
Draught of Immortality, Glacier ice, Health potion, Invisibility potion
Reverse order:
Invisibility potion, Health potion, Glacier ice, Draught of Immortality
size
返回字符串或数组的大小。
- 字符串:字符串的大小是该字符串包含的字符数。
 - 数组:数组的大小是数组中子项的数量。
 
// 1.数据
{
  "collection": {
    "products": [],
    "title": "Sale potions"
  }
}
// 2.代码
{{ collection.title | size }}
{{ collection.products | size }}
// size当您需要在标签或对象输出中使用它时,可以使用带有点符号的过滤器。
{{ collection.products.size }}
// 3.输出
12
0
0
sort
按区分大小写的字母或数字顺序对数组中的项目进行排序。
- 排序区分大小写
 - 先对大写排序,然后在对小写字母排序(大写字母在前面,小写字母在后面)
 
// 1.数据
{
  "collection": {
    "all_tags": [
      "Burning",
      "dried",
      "extra-potent",
      "extracts",
      "fresh",
      "healing",
      "ingredients",
      "music",
      "plant",
      "Salty",
      "supplies"
    ]
  }
}
// 2.代码
{% assign tags = collection.all_tags | sort %}
{% for tag in tags -%}
  {{ tag }}
{%- endfor %}
// 3.输出
Burning
Salty
dried
extra-potent
extracts
fresh
healing
ingredients
music
plant
supplies
按数组项属性排序
- 您可以指定数组项的属性来对数组项进行排序。您可以根据要排序的对象的任何属性进行排序。
 
// 1.数据
{
  "collection": {
    "products": [
      {
        "price": "10.00",
        "title": "Blue Mountain Flower"
      },
      {
        "price": "0.00",
        "title": "Charcoal"
      },
      {
        "price": "56.00",
        "title": "Crocodile tears"
      }
    ]
  }
}
// 2.代码
{% assign products = collection.products | sort: 'price' %}
{% for product in products -%}
  {{ product.title }}
{%- endfor %}
// 3.输出
Charcoal
Blue Mountain Flower
Crocodile tears
sort_natural
按不区分大小写的字母顺序对数组中的项目进行排序。
使用和sort类似
{% assign tags = collection.all_tags | sort_natural %}
{% for tag in tags -%}
  {{ tag }}
{%- endfor %}
sum
返回数组中所有元素的总和。
// 1.代码
{% assign fibonacci = '0, 1, 1, 2, 3, 5' | split: ', ' %}
{{ fibonacci | sum }}
// 2.输出
12
对对象属性值求和
// 1.数据
{
  "cart": {
    "items": [
      {
        "final_line_price": "22.49",
        "quantity": 1
      },
      {
        "final_line_price": "400.00",
        "quantity": 1
      }
    ]
  }
}
// 2.代码
Total quantity of all items in cart:
{{ cart.items | sum: 'quantity' }}
Subtotal price for all items in cart:
{{ cart.items | sum: 'final_line_price' | currency_symbol }}
// 3.输出
Total quantity of all items in cart:
2
Subtotal price for all items in cart:
$422.49
uniq
删除数组中所有重复的项目。
// 1.代码
{% assign potion_array = 'invisibility, health, love, health, invisibility' | split: ', ' %}
{{ potion_array | uniq | join: ', ' }}
// 2.输出
invisibility, health, love
where
过滤数组以仅包含具有特定属性值的项目。
- 这要求您提供属性名称和关联值。
 
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Blue Mountain Flower",
        "vendor": "Polina's Potent Potions"
      },
      {
        "title": "Charcoal",
        "vendor": "Ted's Apothecary Supply"
      },
      {
        "title": "Crocodile tears",
        "vendor": "Polina's Potent Potions"
      }
    ]
  }
}
// 2.代码
{% assign polina_products = collection.products | where: 'vendor', "Polina's Potent Potions" %}
Products from Polina's Potent Potions:
{% for product in polina_products -%}
  - {{ product.title }}
{%- endfor %}
// 3.输出
Products from Polina's Potent Potions:
- Blue Mountain Flower
- Crocodile tears
array_append
将子项A加入到数组B中,该子项可以是字符串,也可以是数组,加入后 子项A 将作为数组B的一个子项、
// 1.数据
{
  "collection": {
    "products": [
      {
        "title": "Blue Mountain Flower",
        "vendor": "Polina's Potent Potions"
      },
      {
        "title": "Charcoal",
        "vendor": "Ted's Apothecary Supply"
      }
    ],
    "product_a": {
        "title": "apple",
        "vendor": "apple girl"
      }
  }
}
// 2.代码
{% assign polina_products = collection.products | array_append: collection.product_a %}
Products from Polina's Potent Potions:
{% for product in polina_products -%}
  - {{ product.title }}
{%- endfor %}
// 3.输出
Products from Polina's Potent Potions:
- Blue Mountain Flower
- Crocodile tears
- apple