find_exp
Finds the first item in an array that matches a Liquid expression. Unlike find, which compares a property to a value, find_exp lets you write arbitrary conditions using the full Liquid expression syntax.
{% assign expensive = products | find_exp: "product", "product.price > 100" %}
{% log expensive.title %}Syntax
{{ array | find_exp: item_name, expression }}array
Array of objects to search
item_name
Variable name for the current item inside the expression
expression
Liquid expression that evaluates to truthy/falsy
Return Value
Returns the first item for which the expression evaluates to a truthy value, or null if no item matches.
Examples
Find first product above a price threshold:
{% assign expensive = products | find_exp: "p", "p.price > 50" %}
{% if expensive %}
{% log "First expensive product: " | append: expensive.title %}
{% endif %}Find first order with a specific tag:
{% assign vip_order = orders | find_exp: "order", "order.tags contains 'VIP'" %}
{% log vip_order.name %}Find first out-of-stock variant:
Find first item matching multiple conditions:
Notes
The
item_nameparameter defines how you reference each item inside the expressionThe expression supports all Liquid operators:
==,!=,>,<,>=,<=,contains,and,orReturns the first matching item only
For simple property-to-value comparisons,
findis more conciseSee also:
where_exp(returns all matching items, not just the first)
Last updated