<p>You might want to rethink your approach – any solution you build for this will break as soon as someone adds a new category that doesn't exactly match the naming pattern. In general, try to avoid redundancies. If the whole category is called <em>Stages</em> or <em>Key Stages</em>, calling each category <em>Key Stage 1</em>, <em>Key Stage 2</em> etc is redundant. If you instead call your categories only <em>1</em>, <em>2</em>, and so on, you avoid the problem altogether. Given that naming, the code becomes nice and easy:</p>
<pre class="lang-twig prettyprint-override"><code>{% set eventKeyStages = event.keyStage.all() %}
{{ 'Key Stage ' ~ (eventKeyStages|join(', '))
</code></pre>
<p>Another advantages of this approach is that it's easy to translate if you ever need more than one langugage:</p>
<pre class="lang-twig prettyprint-override"><code>{% set eventKeyStages = event.keyStage.all() %}
{{ 'Key Stage {stages}'|t({ stages: eventKeyStages|column('title')|join(' &amp; ') }) }}
</code></pre>
<hr />
<p>If you absolutely want to go with your approach, here's a simple solution:</p>
<pre class="lang-twig prettyprint-override"><code>{% set eventKeyStages = event.keyStage.all() %}
{{ 'Key Stage ' ~ (eventKeyStages
    |column('title')
    |map(stage =&gt; stage|replace({ 'Key Stage ': ''}))
    |join(' &amp; ')
) }}
</code></pre>
<hr />
<p>Some additional thoughts addressing the comments and further questions.</p>
<h3>Filtering out categories without &quot;Key Stage&quot; in the name.</h3>
<p>If there are other categories that don't follow that schema, you can adjust your query to only find categories that do:</p>
<pre><code>{% set eventKeyStages = event.keyStage.title('Key Stage*').all() %}
{{ 'Key Stage ' ~ (eventKeyStages|join(', '))
</code></pre>
<p>Of course, you can also get all categories and then split them into to groups (key stage categories and other categories) to display them separately.</p>
<h3>Sorting in ascending orders</h3>
<p>If you're worried that the key stages might be selected in the wrong order, you can order the query by title to make sure they're in the right order:</p>
<pre><code>{% set eventKeyStages = event.keyStage.title('Key Stage*').orderBy('title ASC').all() %}
</code></pre>

CC BY-SA 4.0
2021-11-14T14:11:50.223Z
Thank you for all your help on this! I was able to merge my accounts and have accepted your answer :)
Grace
CC BY-SA 4.0
2021-11-10T17:05:50.557Z
2021-11-12T11:55:57.797Z
2021-11-12T11:55:57.797Z
MoritzLost
<p>I have written a query to select all categories for a certain entry. In this case, the category is called &quot;Key Stage&quot; and each one is written &quot;Key Stage 1&quot;, &quot;Key Stage 2&quot; etc. What I'd like to happen is that if more than one category is selected for the entry, the duplicated words &quot;Key Stage&quot; could be stripped out, leaving &quot;Key Stage 1 &amp; 2&quot;, instead of &quot;Key Stage 1 &amp; Key Stage 2&quot;.</p>
<p>Below is the code I have thus far, which is just the query and looping out the results, adding an ampersand for subsequent items in the loop after the first:</p>
<pre><code>{% set eventKeyStage = event.keyStage.all() %}
&lt;p class=&quot;stage&quot;&gt;
  &lt;strong&gt;
  {% for stage in eventKeyStage %}
  {% if not loop.first %}&amp;amp; {% endif %}
  {{ stage.title }}
  {% endfor %}
  &lt;/strong&gt;
&lt;/p&gt;
</code></pre>
<p>Any advice is greatly appreciated!</p>

<query><array><string>
Remove duplicate text from query results
# Remove duplicate text from query results

(Asked by: username_0 on 2021-11-10)

I have written a query to select all categories for a certain entry. In this case, the category is called "Key Stage" and each one is written "Key Stage 1", "Key Stage 2" etc. What I'd like to happen is that if more than one category is selected for the entry, the duplicated words "Key Stage" could be stripped out, leaving "Key Stage 1 & 2", instead of "Key Stage 1 & Key Stage 2".
Below is the code I have thus far, which is just the query and looping out the results, adding an ampersand for subsequent items in the loop after the first:
{% set eventKeyStage = event.keyStage.all() %}

  
  {% for stage in eventKeyStage %}
  {% if not loop.first %}& {% endif %}
  {{ stage.title }}
  {% endfor %}
  


Any advice is greatly appreciated!

**Tags:** query, array, string


---

## 1 answers

### Answer by username_1 on 2021-11-10. Score: 2 | Accepted answer

You might want to rethink your approach – any solution you build for this will break as soon as someone adds a new category that doesn't exactly match the naming pattern. In general, try to avoid redundancies. If the whole category is called Stages or Key Stages, calling each category Key Stage 1, Key Stage 2 etc is redundant. If you instead call your categories only 1, 2, and so on, you avoid the problem altogether. Given that naming, the code becomes nice and easy:
{% set eventKeyStages = event.keyStage.all() %}
{{ 'Key Stage ' ~ (eventKeyStages|join(', '))

Another advantages of this approach is that it's easy to translate if you ever need more than one langugage:
{% set eventKeyStages = event.keyStage.all() %}
{{ 'Key Stage {stages}'|t({ stages: eventKeyStages|column('title')|join(' & ') }) }}


If you absolutely want to go with your approach, here's a simple solution:
{% set eventKeyStages = event.keyStage.all() %}
{{ 'Key Stage ' ~ (eventKeyStages
    |column('title')
    |map(stage => stage|replace({ 'Key Stage ': ''}))
    |join(' & ')
) }}


Some additional thoughts addressing the comments and further questions.
Filtering out categories without "Key Stage" in the name.
If there are other categories that don't follow that schema, you can adjust your query to only find categories that do:
{% set eventKeyStages = event.keyStage.title('Key Stage*').all() %}
{{ 'Key Stage ' ~ (eventKeyStages|join(', '))

Of course, you can also get all categories and then split them into to groups (key stage categories and other categories) to display them separately.
Sorting in ascending orders
If you're worried that the key stages might be selected in the wrong order, you can order the query by title to make sure they're in the right order:
{% set eventKeyStages = event.keyStage.title('Key Stage*').orderBy('title ASC').all() %}

> **Comments:**
> **username_0**: Thank you for all your help on this! I was able to merge my accounts and have accepted your answer :)  

---

