Answers.Body: <p>Twig isn't very well equipped to deal with more than basic logic and comparisons, but you can accomplish some of these tests with a bit of extra juggling:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set constraintMet = false %}
  
  {% for value in [0, 30, 50, 10, 9, 200] %}
    {# Here’s where you get to set up your advanced comparison criteria: #}
    {% if not constraintMet and value &lt; 10 %}
      {% set constraintMet = true %}
    {% endif %}
  {% endfor %}
  
  Constraint {{ constraintMet ? "was" : "wasn't" }} met.
  </code></pre>
  
  <p>Alternatively, you can build the conditional into your <code>for</code> loop…</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set constraintMet = false %}
  
  {% for value in [0, 30, 50, 10, 9, 200] if not constraintMet and value &lt; 10 %}
    {% set constraintMet = true %}
  {% endfor %}
  
  Constraint {{ constraintMet ? "was" : "wasn't" }} met.
  </code></pre>
  
  <p>…but this is a little more difficult to read, in my opinion, as it obscures the critical test. One improvement this offers, though, is that it prevents the loop from running after the constraint is met, unlike the first option, which runs/renders the body of the <code>if</code> block for every element in the array—even if it finds a match early on.</p>
  
  <p>Whenever possible, I'd recommend stashing this kind of logic in a custom <a href="https://docs.craftcms.com/v3/extend/extending-twig.html#register-a-twig-extension" rel="nofollow noreferrer">Twig extension</a>. If you haven't built a plugin or module before, <a href="https://pluginfactory.io" rel="nofollow noreferrer">Pluginfactory</a> can set you down the right path!</p>
  
  <blockquote>
    <p>Heads up! <code>map</code>, <code>filter</code>, and <code>reduce</code> functions have come to Twig (and Craft, as of 3.2), which make this possible to collapse this into a one-line statement: <code>{% set hasMatchingValue = [0, 30, 50, 10, 9, 200] | filter(v =&gt; v &lt; 10) | length &gt; 0 %}</code>. This just iterates over the array, and filters out anything that is 10 or greater with the implicitly returned value from the “arrow function,” then checks if the new array includes “more than zero” values.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-28T08:51:05.993Z
Answers.Text: thanks. I always want to look into how to build extensions. but then daily business happens :(
Answers.UserDisplayName: KSPR
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-28T06:27:01.507Z
Answers.LastActivityDate: 2019-07-11T22:47:53.407Z
Answers.LastEditDate: 2019-07-11T22:47:53.407Z
Answers.UserDisplayName: August Miller
Body: <p>I want to check if there is a number less than 10 in an array.</p>
  
  <pre><code>{% if 10 in [0, 30, 50, 10, 9, 200]  %}
    {# checks only if there is a 10 in it. not if there is anything below 10. #}
     yep. there is a number 10
  {% endif %}
  </code></pre>
  
  <p>That's close, but I want to find find out if there is any number <em>below</em> 10 in it, disregarding how many.</p>
  
  <p>Not possible: <code>{% if 10 &gt; in [0, 30, 50, 10, 9, 200] %}</code></p>
  
  <p>Also not possible: <code>{% if &gt; 10 in [0, 30, 50, 10, 9, 200] %}</code></p>
  
Tags: <templating><conditional><array>
Title: Check for number less than x in array
ThreadText: # Check for number less than x in array
  
  (Asked by: username_0 on 2019-06-27)
  
  I want to check if there is a number less than 10 in an array.
  
  ```
  {% if 10 in [0, 30, 50, 10, 9, 200]  %}
    {# checks only if there is a 10 in it. not if there is anything below 10. #}
     yep. there is a number 10
  {% endif %}
  
  ```
  
  That's close, but I want to find find out if there is any number *below* 10 in it, disregarding how many.
  
  Not possible: `{% if 10 > in [0, 30, 50, 10, 9, 200] %}`
  
  Also not possible: `{% if > 10 in [0, 30, 50, 10, 9, 200] %}`
  
  **Tags:** templating, conditional, array
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-06-28. Score: 3 | Accepted answer
  
  Twig isn't very well equipped to deal with more than basic logic and comparisons, but you can accomplish some of these tests with a bit of extra juggling:
  
  ```twig
  {% set constraintMet = false %}
  
  {% for value in [0, 30, 50, 10, 9, 200] %}
    {# Here’s where you get to set up your advanced comparison criteria: #}
    {% if not constraintMet and value < 10 %}
      {% set constraintMet = true %}
    {% endif %}
  {% endfor %}
  
  Constraint {{ constraintMet ? "was" : "wasn't" }} met.
  
  ```
  
  Alternatively, you can build the conditional into your `for` loop…
  
  ```twig
  {% set constraintMet = false %}
  
  {% for value in [0, 30, 50, 10, 9, 200] if not constraintMet and value < 10 %}
    {% set constraintMet = true %}
  {% endfor %}
  
  Constraint {{ constraintMet ? "was" : "wasn't" }} met.
  
  ```
  
  …but this is a little more difficult to read, in my opinion, as it obscures the critical test. One improvement this offers, though, is that it prevents the loop from running after the constraint is met, unlike the first option, which runs/renders the body of the `if` block for every element in the array—even if it finds a match early on.
  
  Whenever possible, I'd recommend stashing this kind of logic in a custom [Twig extension](https://docs.craftcms.com/v3/extend/extending-twig.html#register-a-twig-extension). If you haven't built a plugin or module before, [Pluginfactory](https://pluginfactory.io) can set you down the right path!
  
  > Heads up! `map`, `filter`, and `reduce` functions have come to Twig (and Craft, as of 3.2), which make this possible to collapse this into a one-line statement: `{% set hasMatchingValue = [0, 30, 50, 10, 9, 200] | filter(v => v < 10) | length > 0 %}`. This just iterates over the array, and filters out anything that is 10 or greater with the implicitly returned value from the “arrow function,” then checks if the new array includes “more than zero” values.
  
  > **Comments:**
  > **username_0**: thanks. I always want to look into how to build extensions. but then daily business happens :(  
  
  ---
  
  