Liquid now supports find, find_index, has, and reject filters for arrays!
At first glance, this might seem like a small change, but these additions drastically reduce code complexity, making logic cleaner and improving performance by eliminating unnecessary loops and conditions.
Shopify Liquid provides powerful filters that allow developers to manipulate arrays and collections efficiently.
In this article, we will explore four new useful Shopify Array filters: find, find_index, has, and reject.
We will learn how we can use these filters in different type of data like Shopify arrays, Shopify product collections or product list, and Shopify metaobjects list.
The find filter searches for the first occurrence of a specified value in an array. It is useful when looking for a specific item within a list.
How to use Shopify find filter for liquid array
{% assign products = "Shirt, Shoes, Hat, Jacket" | split: ", " %}
{% assign found_item = products | find: "Sh" %}
<div>{{ found_item }}</div>
Learn how you can use Shopify find filter for metaobjects list
Metaobjects store additional structured data, and filters can be applied to retrieve relevant information
{% assign found_product = shop.metaobjects.custom_products.values | find: "category", "Footwear" %}
{% if found_product %}
<p>Found: {{ found_product.name }} - {{ found_product.category }} - ${{ found_product.price }}</p>
{% else %}
<p>Product not found</p>
{% endif %}
Benefit: Quickly retrieves a matching item from an array, reducing the need for manual loops.
How to use Shopify find_index filter for liquid array
The find_index filter returns the index of a specific item within an array or collection.
Sample code for has shopify find_index filter
{% assign index = products | find_index: "Shoes" %}
<div>{{ index }}</div>
How to use shopify find_index filter for Shopify metaobject list
{% assign product_index = shop.metaobjects.custom_products.values | find_index: "category", "Footwear" %}
{% if product_index != nil %}
<p>Index: {{ product_index }}</p>
{% else %}
<p>Product not found</p>
{% endif %}
Has Filter
The has filter checks whether an item exists within an array or collection.
Sample code for has shopify has filter
{% assign has_shoes = products | has: "Shoes" %}
{{ has_shoes }}<br>
Metaobjects
{% assign product_exists = shop.metaobjects.custom_products.values | has: "category", "Clothing" %}{% if product_exists %}
<p>Product Exists</p>
{% else %}
<p>Product Not Found</p>
{% endif %}
4. Reject Filter
The reject filter removes elements from an array based on specified criteria.
Example Usage:
1. Normal Arrays
{% assign filtered_products = products | reject: "Hat" %}
{{ filtered_products | join: ", " }}<br>
3. Metaobjects
{% assign filtered_products = shop.metaobjects.custom_products.values | reject: "category", "Clothing" %}
{% for product in filtered_products %}
<p>{{ product.name }}</p>
{% endfor %}
Pros and Cons of Shopify Liquid Filters
- Finds Products Faster – Helps quickly locate specific items in a list, saving time.
- Improves Website Speed – Makes filtering and searching more efficient, reducing unnecessary work.
- Works in Multiple Places – Can be used for product lists, collections, and extra data (metaobjects).
- Better Shopping Experience – Helps customers find what they need quickly, leading to more sales.
- Cleaner Code – Makes coding simpler and easier to read compared to manual searching.
Cons (Disadvantages)
- Case-Sensitive – If the search term doesn't match exactly (uppercase/lowercase), it won’t find the item.
- Finds Only the First Match – If there are multiple matches, it stops at the first one, which may not always be ideal.
- Limited Advanced Filtering – Not as powerful as database searches for complex filtering needs.
- Not Ideal for Large Stores – May slow down performance if used with thousands of products.