|
| 1 | +--- |
| 2 | +title: where |
| 3 | +description: Liquid filter that selects from arrays. |
| 4 | +--- |
| 5 | + |
| 6 | +Creates an array including only the objects with a given property value, or any [truthy]({{ "/basics/truthy-and-falsy#truthy" | prepend: site.baseurl }}) value by default. |
| 7 | + |
| 8 | +In this example, assume you have a list of products and you want to show your kitchen products separately. Using `where`, you can create an array containing only the products that have a `"type"` of `"kitchen"`. |
| 9 | + |
| 10 | +<p class="code-label">Input</p> |
| 11 | +{% raw %} |
| 12 | +```liquid |
| 13 | +All products: |
| 14 | +{% for product in products %} |
| 15 | +- {{ product.title }} |
| 16 | +{% endfor %} |
| 17 | +
|
| 18 | +{% assign kitchen_products = products | where: "type", "kitchen" %} |
| 19 | +
|
| 20 | +Kitchen products: |
| 21 | +{% for product in kitchen_products %} |
| 22 | +- {{ product.title }} |
| 23 | +{% endfor %} |
| 24 | +``` |
| 25 | +{% endraw %} |
| 26 | + |
| 27 | +<p class="code-label">Output</p> |
| 28 | +```text |
| 29 | +All products: |
| 30 | +- Vacuum |
| 31 | +- Spatula |
| 32 | +- Television |
| 33 | +- Garlic press |
| 34 | +
|
| 35 | +Kitchen products: |
| 36 | +- Spatula |
| 37 | +- Garlic press |
| 38 | +
|
| 39 | +``` |
| 40 | + |
| 41 | +Say instead you have a list of products and you only want to show those that are available to buy. You can `where` with a property name but no target value to include all products with a [truthy]({{ "/basics/truthy-and-falsy#truthy" | prepend: site.baseurl }}) `"available"` value. |
| 42 | + |
| 43 | +<p class="code-label">Input</p> |
| 44 | +{% raw %} |
| 45 | +```liquid |
| 46 | +All products: |
| 47 | +{% for product in products %} |
| 48 | +- {{ product.title }} |
| 49 | +{% endfor %} |
| 50 | +
|
| 51 | +{% assign available_products = products | where: "available" %} |
| 52 | +
|
| 53 | +Available products: |
| 54 | +{% for product in available_products %} |
| 55 | +- {{ product.title }} |
| 56 | +{% endfor %} |
| 57 | +
|
| 58 | +``` |
| 59 | +{% endraw %} |
| 60 | + |
| 61 | +<p class="code-label">Output</p> |
| 62 | + |
| 63 | +```text |
| 64 | +All products: |
| 65 | +- Coffee mug |
| 66 | +- Limited edition sneakers |
| 67 | +- Boring sneakers |
| 68 | +
|
| 69 | +Available products: |
| 70 | +- Coffee mug |
| 71 | +- Boring sneakers |
| 72 | +
|
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | +The `where` filter can also be used to find a single object in an array when combined with the `first` filter. For example, say you want to show off the shirt in your new fall collection. |
| 77 | + |
| 78 | +<p class="code-label">Input</p> |
| 79 | +{% raw %} |
| 80 | +```liquid |
| 81 | +{% assign new_shirt = products | where: "type", "shirt" | first %} |
| 82 | +
|
| 83 | +Featured product: {{ new_shirt.title }} |
| 84 | +``` |
| 85 | +{% endraw %} |
| 86 | + |
| 87 | +<p class="code-label">Output</p> |
| 88 | +```text |
| 89 | +Featured product: Hawaiian print sweater vest |
| 90 | +``` |
0 commit comments