v10.21.0
Creates an array excluding the objects with a given property value, or excluding truthy values by default when a property is not given.
In this example, assume you have a list of products and you want to filter out kitchen products. Using reject
, you can create an array excluding only the products that have a "type"
of "kitchen"
.
Input
All products:
{% for product in products %}
- {{ product.title }}
{% endfor %}
{% assign non_kitchen_products = products | reject: "type", "kitchen" %}
Kitchen products:
{% for product in non_kitchen_products %}
- {{ product.title }}
{% endfor %}
Output
All products:
- Vacuum
- Spatula
- Television
- Garlic press
Kitchen products:
- Vacuum
- Television
Say instead you have a list of products and you want to exclude taxable products. You can reject
with a property name but no target value to reject all products with a truthy "taxable"
value.
Input
All products:
{% for product in products %}
- {{ product.title }}
{% endfor %}
{% assign not_taxed_products = products | reject: "taxable" %}
Available products:
{% for product in not_taxed_products %}
- {{ product.title }}
{% endfor %}
Output
All products:
- Vacuum
- Spatula
- Television
- Garlic press
Available products:
- Spatula
- Television
Additionally, property
can be any valid Liquid variable expression as used in output syntax, except that the scope of this expression is within each item. For the following products
array:
const products = [
{ meta: { details: { class: 'A' } }, order: 1 },
{ meta: { details: { class: 'B' } }, order: 2 },
{ meta: { details: { class: 'B' } }, order: 3 }
]
Input
{% assign selected = products | reject: 'meta.details["class"]', "B" %}
{% for item in selected -%}
- {{ item.order }}
{% endfor %}
Output
- 1
Jekyll style
v10.21.0
For Liquid users migrating from Jekyll, there’s a jekyllWhere
option to mimic the behavior of Jekyll’s where
filter. This option is set to false
by default. When enabled, if property
is an array, the target value is matched using Array.includes
instead of ==
, which is particularly useful for excluding tags.
const pages = [
{ tags: ["cat", "food"], title: 'Cat Food' },
{ tags: ["dog", "food"], title: 'Dog Food' },
]
Input
{% assign selected = pages | reject: 'tags', "cat" %}
{% for item in selected -%}
- {{ item.title }}
{% endfor %}
Output
Dog Food