v8.1.0
Creates an array including only the objects with a given property value, or any truthy value by default.
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".
Input
1 | All products: |
Output
1 | All products: |
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 "available" value.
Input
1 | All products: |
Output
1 | All products: |
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.
Input
1 | {% assign new_shirt = products | where: "type", "shirt" | first %} |
Output
1 | Featured product: Hawaiian print sweater vest |
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:
1 | const products = [ |
Input
1 | {% assign selected = products | where: 'meta.details["class"]', "B" %} |
Output
1 | - 2 |