|
| 1 | +--- |
| 2 | +title: reject |
| 3 | +--- |
| 4 | + |
| 5 | +{% since %}v10.21.0{% endsince %} |
| 6 | + |
| 7 | +Creates an array excluding the objects with a given property value, or excluding [truthy][truthy] values by default when a property is not given. |
| 8 | + |
| 9 | +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"`. |
| 10 | + |
| 11 | +Input |
| 12 | +```liquid |
| 13 | +All products: |
| 14 | +{% for product in products %} |
| 15 | +- {{ product.title }} |
| 16 | +{% endfor %} |
| 17 | +
|
| 18 | +{% assign non_kitchen_products = products | reject: "type", "kitchen" %} |
| 19 | +
|
| 20 | +Kitchen products: |
| 21 | +{% for product in non_kitchen_products %} |
| 22 | +- {{ product.title }} |
| 23 | +{% endfor %} |
| 24 | +``` |
| 25 | + |
| 26 | +Output |
| 27 | +```text |
| 28 | +All products: |
| 29 | +- Vacuum |
| 30 | +- Spatula |
| 31 | +- Television |
| 32 | +- Garlic press |
| 33 | +
|
| 34 | +Kitchen products: |
| 35 | +- Vacuum |
| 36 | +- Television |
| 37 | +``` |
| 38 | + |
| 39 | +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][truthy] `"taxable"` value. |
| 40 | + |
| 41 | +Input |
| 42 | +```liquid |
| 43 | +All products: |
| 44 | +{% for product in products %} |
| 45 | +- {{ product.title }} |
| 46 | +{% endfor %} |
| 47 | +
|
| 48 | +{% assign not_taxed_products = products | reject: "taxable" %} |
| 49 | +
|
| 50 | +Available products: |
| 51 | +{% for product in not_taxed_products %} |
| 52 | +- {{ product.title }} |
| 53 | +{% endfor %} |
| 54 | +``` |
| 55 | + |
| 56 | +Output |
| 57 | +```text |
| 58 | +All products: |
| 59 | +- Vacuum |
| 60 | +- Spatula |
| 61 | +- Television |
| 62 | +- Garlic press |
| 63 | +
|
| 64 | +Available products: |
| 65 | +- Spatula |
| 66 | +- Television |
| 67 | +``` |
| 68 | + |
| 69 | +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: |
| 70 | + |
| 71 | +```javascript |
| 72 | +const products = [ |
| 73 | + { meta: { details: { class: 'A' } }, order: 1 }, |
| 74 | + { meta: { details: { class: 'B' } }, order: 2 }, |
| 75 | + { meta: { details: { class: 'B' } }, order: 3 } |
| 76 | +] |
| 77 | +``` |
| 78 | + |
| 79 | +Input |
| 80 | +```liquid |
| 81 | +{% assign selected = products | reject: 'meta.details["class"]', "B" %} |
| 82 | +{% for item in selected -%} |
| 83 | +- {{ item.order }} |
| 84 | +{% endfor %} |
| 85 | +``` |
| 86 | + |
| 87 | +Output |
| 88 | +```text |
| 89 | +- 1 |
| 90 | +``` |
| 91 | + |
| 92 | +## Jekyll style |
| 93 | + |
| 94 | +{% since %}v10.21.0{% endsince %} |
| 95 | + |
| 96 | +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. |
| 97 | + |
| 98 | +```javascript |
| 99 | +const pages = [ |
| 100 | + { tags: ["cat", "food"], title: 'Cat Food' }, |
| 101 | + { tags: ["dog", "food"], title: 'Dog Food' }, |
| 102 | +] |
| 103 | +``` |
| 104 | + |
| 105 | +Input |
| 106 | +```liquid |
| 107 | +{% assign selected = pages | reject: 'tags', "cat" %} |
| 108 | +{% for item in selected -%} |
| 109 | +- {{ item.title }} |
| 110 | +{% endfor %} |
| 111 | +``` |
| 112 | + |
| 113 | +Output |
| 114 | +```text |
| 115 | +Dog Food |
| 116 | +``` |
| 117 | + |
| 118 | +[truthy]: ../tutorials/truthy-and-falsy.html |
0 commit comments