<p>You could consider twig's <code>|filter</code> to filter your array.</p>
<pre><code>{% set reviewPages = craft.entries.section(&quot;reviews&quot;).all() %}
{% set validReviewPages = reviewPages|filter(page =&gt; {
    page.fieldX|length &gt; 0
}) %}
</code></pre>
<p>Then you have a validReviewPages Array which you can iterate through.</p>

CC BY-SA 4.0
2023-11-21T00:14:36.123Z
That gets me closer, but then when I get to `{% set next = validPages[loop.index0+2] %}` I get "Key "5" for array with keys "1, 2, 3, 4, 6, 7, 9, 10, 11" does not exist."
Tyssen
CC BY-SA 4.0
2023-11-22T06:12:41.617Z
@Tyssen try to use values filter after filtering the data.
```{% set validReviewPages = reviewPages|filter(page => {
    page.fieldX|length > 0
})|values %}```
aodihis
CC BY-SA 4.0
2023-11-20T10:43:53.063Z
2023-11-20T10:43:53.063Z
Max Strebel
<p>I'm working on a reviews site where each entry has a series of fields, but on the front end, different pages are used depending on whether fields have values in them or not, e.g. if <code>fieldX</code> has data, on the front end you can access a page at a URL like <code>/review/entryName/fieldX</code>. And links to those pages appear if <code>fieldX|length</code>.</p>
<p>But now I need to create next and prev page links. If <code>field1</code> doesn't have a value, then you have to keep going through the list of possible fields that might have their own page to find one which has a value to create the next link, e.g. it might be <code>field5</code> having skipped out 2, 3, 4.</p>
<p>What I have so far is</p>
<pre><code>{% set reviewPages = {
  0: {
    'field': entry.field1|length,
    'text': 'Field 1',
    'url': 'field_1'
  },
  1: {
    'field': entry.field2|length,
    'text': 'Field 2',
    'url': 'field_2'
  }
}
%}

{% set prev = null %}
{% set next = reviewPages[1] %}

{% for page in reviewPages %}
  {% if page.url == segment_3 %}
    {% if prev is not null and (prev.field &gt; 0) %}
      &lt;a href=&quot;{{ entry.url }}/{{ prev.url }}&quot;&gt;{{ prev.text }}&lt;/a&gt;
    {% else %}
        {% continue %}
    {% endif %}
    {% if next is not null and next.field &gt; 0 %}
      &lt;a href=&quot;{{ entry.url }}/{{ next.url }}&quot;&gt;{{ next.text }}&lt;/a&gt;
    {% else %}
        {% continue %}
    {% endif %}
  {% endif %}
  {% set prev = page %}
      {% if loop.revindex &lt;= 2 %}
         {% set next = null %}
      {% else %}
          {% set next = reviewPages[loop.index0+2] %}
      {%endif%}
{% endfor %}
</code></pre>
<p>This works fine if all the fields have data, but breaks down when it encounters a next field which doesn't have any.</p>
<p>I need something for the loop to continue on to the next item in the array and test that and so on until it finds one with data.</p>
<p>I thought I might be able to use <a href="https://plugins.craftcms.com/twig-perversion" rel="nofollow noreferrer">https://plugins.craftcms.com/twig-perversion</a> which provides a <code>continue</code> tag but it doesn't work which is probably because the first line in the loop is checking whether the url of the loop item matches the url of the page you're on and it will only match for one item in the array.</p>

<link>
Next previous page links
# Next previous page links

(Asked by: username_0 on 2023-11-20)

I'm working on a reviews site where each entry has a series of fields, but on the front end, different pages are used depending on whether fields have values in them or not, e.g. if fieldX has data, on the front end you can access a page at a URL like /review/entryName/fieldX. And links to those pages appear if fieldX|length.
But now I need to create next and prev page links. If field1 doesn't have a value, then you have to keep going through the list of possible fields that might have their own page to find one which has a value to create the next link, e.g. it might be field5 having skipped out 2, 3, 4.
What I have so far is
{% set reviewPages = {
  0: {
    'field': entry.field1|length,
    'text': 'Field 1',
    'url': 'field_1'
  },
  1: {
    'field': entry.field2|length,
    'text': 'Field 2',
    'url': 'field_2'
  }
}
%}

{% set prev = null %}
{% set next = reviewPages[1] %}

{% for page in reviewPages %}
  {% if page.url == segment_3 %}
    {% if prev is not null and (prev.field > 0) %}
      {{ prev.text }}
    {% else %}
        {% continue %}
    {% endif %}
    {% if next is not null and next.field > 0 %}
      {{ next.text }}
    {% else %}
        {% continue %}
    {% endif %}
  {% endif %}
  {% set prev = page %}
      {% if loop.revindex <= 2 %}
         {% set next = null %}
      {% else %}
          {% set next = reviewPages[loop.index0+2] %}
      {%endif%}
{% endfor %}

This works fine if all the fields have data, but breaks down when it encounters a next field which doesn't have any.
I need something for the loop to continue on to the next item in the array and test that and so on until it finds one with data.
I thought I might be able to use https://plugins.craftcms.com/twig-perversion which provides a continue tag but it doesn't work which is probably because the first line in the loop is checking whether the url of the loop item matches the url of the page you're on and it will only match for one item in the array.

**Tags:** link

---

## 1 answers

### Answer by username_1 on 2023-11-20. Score: 1

You could consider twig's |filter to filter your array.
{% set reviewPages = craft.entries.section("reviews").all() %}
{% set validReviewPages = reviewPages|filter(page => {
    page.fieldX|length > 0
}) %}

Then you have a validReviewPages Array which you can iterate through.

> **Comments:**
> **username_0**: That gets me closer, but then when I get to `{% set next = validPages[loop.index0+2] %}` I get "Key "5" for array with keys "1, 2, 3, 4, 6, 7, 9, 10, 11" does not exist."  
> **username_2**: @Tyssen try to use values filter after filtering the data. ```{% set validReviewPages = reviewPages|filter(page => {     page.fieldX|length > 0 })|values %}```  

---

