Answers.Body: <p>You're looking for the <code>filter</code> filter: <a href="https://docs.craftcms.com/v3/dev/filters.html#filter" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/filters.html#filter</a></p>
  <p>Specifically something like this (untested):</p>
  <pre><code>{% set activeChildren = children|filter(child =&gt; child.associatedTerm|length) %}
  {% set inactiveChildren = children|filter(child =&gt; child.associatedTerm|length == 0) %}
  </code></pre>
  <p>You now have two unique arrays to do with as you please:</p>
  <pre><code>{% if activeChildren|length %}
     &lt;h2&gt;You have active children&lt;/h2&gt;
  {% endif %}
  &lt;p class=&quot;{{ inactiveChildren|length ? 'inactiveChildren' }}&quot;&gt;Hi there&lt;/p&gt;
  </code></pre>
  <p>Whenever you get the sneaky feeling that a loop is overkill, look to <code>map</code> <code>filter</code> and <code>reduce</code>: these 3 methods are all like loops in disguise, in that they iterate through arrays and return something new based on the logic you supply.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-07T11:02:26.117Z
Answers.Text: That's great. Thanks @jamessmith - the filter looks like a great approach.
Answers.UserDisplayName: Terry Upton
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-07T10:38:39.920Z
Answers.LastActivityDate: 2020-07-07T10:38:39.920Z
Answers.UserDisplayName: James Smith
Body: <p>So I have a section called children and these are related to various other entries include users.
  I am fetching all the children for the currentUser like so and eager-loading all the other relationships.</p>
  <pre><code>{% set childrenQuery = craft.entries
    .section('children')
    .relatedTo({
    sourceElement: currentUser,
    field: 'relatedChildren'
  })
    .with([
    'associatedTerm',
    'tuitionClass',
    'tuitionClass.location'
  ]) %}
  
  {% set children = childrenQuery.all() %}
  </code></pre>
  <p>What I then want to do is split out children in to 'Active' and 'Inactive' depending on if the related field associatedTerm is live.</p>
  <p>I can do this fine like this;</p>
  <pre><code>Children (active)&lt;br /&gt;
  &lt;ul class=&quot;list-disc mb-6&quot;&gt;
    {% for child in children if child.associatedTerm|length %}
      &lt;li&gt;{{ child.firstName }} {{ child.surname }}&lt;/li&gt;
      &lt;li&gt;{{ child.tuitionClass[0] }}&lt;/li&gt;
      &lt;li&gt;{{ child.associatedTerm[0] }}&lt;/li&gt;
      &lt;li&gt;{{ child.tuitionClass[0].location[0] }}&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
  
  Children Inactive ()&lt;br /&gt;
    &lt;ul class=&quot;list-disc mb-6&quot;&gt;
      {% for child in children if not child.associatedTerm|length %}
        &lt;li&gt;{{ child.firstName }} {{ child.surname }}&lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;
  </code></pre>
  <p>However, what I want to do is actually do this logic first, so I can then wrap the active/inactive blocks into if statements, if there are in fact any active or inactive children as I have other code (headings and text) I want to show within the block and not appear at all if they are no entries.</p>
  <p>So I am stuck on how to do this {% for child in children if not child.associatedTerm|length %} without a for loop and just check if there are any entries that exist.</p>
  <p>I can achieve it like this; but it feels a little weird.</p>
  <pre><code>{% set childrenAreInactive = '' %}
  {% for child in children if not child.associatedTerm|length %}
    {% set childrenAreInactive = 1 %}
  {% endfor %}
  </code></pre>
  <p>Anyone have any ideas for a better method?</p>
  
Tags: <templating><forloop>
Title: Checking if related entries are live in a for loop and setting a variable based on the results
ThreadText: # Checking if related entries are live in a for loop and setting a variable based on the results
  
  (Asked by: username_0 on 2020-07-07)
  
  So I have a section called children and these are related to various other entries include users.
  I am fetching all the children for the currentUser like so and eager-loading all the other relationships.
  
  ```
  {% set childrenQuery = craft.entries
    .section('children')
    .relatedTo({
    sourceElement: currentUser,
    field: 'relatedChildren'
  })
    .with([
    'associatedTerm',
    'tuitionClass',
    'tuitionClass.location'
  ]) %}
  
  {% set children = childrenQuery.all() %}
  
  ```
  
  What I then want to do is split out children in to 'Active' and 'Inactive' depending on if the related field associatedTerm is live.
  
  I can do this fine like this;
  
  ```
  Children (active)<br />
  <ul class="list-disc mb-6">
    {% for child in children if child.associatedTerm|length %}
      <li>{{ child.firstName }} {{ child.surname }}</li>
      <li>{{ child.tuitionClass[0] }}</li>
      <li>{{ child.associatedTerm[0] }}</li>
      <li>{{ child.tuitionClass[0].location[0] }}</li>
    {% endfor %}
  </ul>
  
  Children Inactive ()<br />
    <ul class="list-disc mb-6">
      {% for child in children if not child.associatedTerm|length %}
        <li>{{ child.firstName }} {{ child.surname }}</li>
      {% endfor %}
    </ul>
  
  ```
  
  However, what I want to do is actually do this logic first, so I can then wrap the active/inactive blocks into if statements, if there are in fact any active or inactive children as I have other code (headings and text) I want to show within the block and not appear at all if they are no entries.
  
  So I am stuck on how to do this {% for child in children if not child.associatedTerm|length %} without a for loop and just check if there are any entries that exist.
  
  I can achieve it like this; but it feels a little weird.
  
  ```
  {% set childrenAreInactive = '' %}
  {% for child in children if not child.associatedTerm|length %}
    {% set childrenAreInactive = 1 %}
  {% endfor %}
  
  ```
  
  Anyone have any ideas for a better method?
  
  **Tags:** templating, forloop
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-07-07. Score: 2 | Accepted answer
  
  You're looking for the `filter` filter: <https://docs.craftcms.com/v3/dev/filters.html#filter>
  
  Specifically something like this (untested):
  
  ```
  {% set activeChildren = children|filter(child => child.associatedTerm|length) %}
  {% set inactiveChildren = children|filter(child => child.associatedTerm|length == 0) %}
  
  ```
  
  You now have two unique arrays to do with as you please:
  
  ```
  {% if activeChildren|length %}
     <h2>You have active children</h2>
  {% endif %}
  <p class="{{ inactiveChildren|length ? 'inactiveChildren' }}">Hi there</p>
  
  ```
  
  Whenever you get the sneaky feeling that a loop is overkill, look to `map` `filter` and `reduce`: these 3 methods are all like loops in disguise, in that they iterate through arrays and return something new based on the logic you supply.
  
  > **Comments:**
  > **username_0**: That's great. Thanks @username_1 - the filter looks like a great approach.  
  
  ---
  
  