Checking if related entries are live in a for loop and setting a variable based on the results 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)&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;

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?
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 =&gt; child.associatedTerm|length) %}
{% set inactiveChildren = children|filter(child =&gt; child.associatedTerm|length == 0) %}

You now have two unique arrays to do with as you please:
{% 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;

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.