<p>When looping over the letters in the range, you should be able to use the <code>keys</code> filter to check if the current letter is in the <code>entriesByGroup</code> array. If it is, you can link it or add a class, etc.</p>
<pre><code>{% set letterArray = 'A'..'Z' %}
{% set entries = craft.entries.section('myEntries').orderBy('title asc').all() %}
{% set entriesByGroup = entries|group('title[:1]') %}

{% for letter in letterArray %}
   {% if letter in entriesByGroup|keys %}
      {# this letter has at least one entry, anchor link, add a class #}
   {% endif %}
{% endfor %}
 
{% for firstLetter, entriesInGroup in entriesByGroup %}
   {# your alphabetical entry listing %}
{% endfor %}
</code></pre>
<p>I do something similar here on the <a href="https://craftquest.io/archive" rel="nofollow noreferrer">CraftQuest Archive page</a> but with a slightly different approach (<a href="https://craftquest.io/courses/creating-content-archives/5423" rel="nofollow noreferrer">here's a video on it</a>).</p>

CC BY-SA 4.0
2021-05-17T03:01:47.103Z
Thanks so much for the help Ryan (assuming it's Ryan ). I wasn't familiar with the `keys` filter. That did what I needed. Will definitely watch the video you linked to.
A Herrera
CC BY-SA 4.0
2021-05-18T19:27:19.873Z
It's me! You're welcome. :)
CraftQuest
CC BY-SA 4.0
2021-05-16T04:34:03.813Z
2021-05-16T04:34:03.813Z
CraftQuest
<p>I'm trying to output the alphabet where certain letters are enabled/clickable based on whether any entries from a specific section start with that letter. Here's the UI:
<a href="https://i.sstatic.net/fkFP9.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/fkFP9.png" alt="enter image description here" /></a></p>
<p>Getting the A-Z is easy enough:</p>
<pre><code>{% set letterArray = 'A'..'Z' %}
</code></pre>
<p>And I'm able to get the entries by first letter:</p>
<pre><code>{% set entries = craft.entries.section('myEntries').orderBy('title asc').all() %}
{% set entriesByGroup = entries|group('title[:1]') %}
{% for firstLetter, entriesInGroup in entriesByGroup %}
    ...
{% endfor %}
</code></pre>
<p>I just can't figure out how to properly merge them so I can set a class or whatever on the letters that have at least an entry.</p>

<craft3><entries><merge>
Modified A-Z grouping
# Modified A-Z grouping

(Asked by: username_0 on 2021-05-16)

I'm trying to output the alphabet where certain letters are enabled/clickable based on whether any entries from a specific section start with that letter. Here's the UI:

Getting the A-Z is easy enough:
{% set letterArray = 'A'..'Z' %}

And I'm able to get the entries by first letter:
{% set entries = craft.entries.section('myEntries').orderBy('title asc').all() %}
{% set entriesByGroup = entries|group('title[:1]') %}
{% for firstLetter, entriesInGroup in entriesByGroup %}
    ...
{% endfor %}

I just can't figure out how to properly merge them so I can set a class or whatever on the letters that have at least an entry.

**Tags:** craft3, entries, merge


---

## 1 answers

### Answer by username_1 on 2021-05-16. Score: 4 | Accepted answer

When looping over the letters in the range, you should be able to use the keys filter to check if the current letter is in the entriesByGroup array. If it is, you can link it or add a class, etc.
{% set letterArray = 'A'..'Z' %}
{% set entries = craft.entries.section('myEntries').orderBy('title asc').all() %}
{% set entriesByGroup = entries|group('title[:1]') %}

{% for letter in letterArray %}
   {% if letter in entriesByGroup|keys %}
      {# this letter has at least one entry, anchor link, add a class #}
   {% endif %}
{% endfor %}
 
{% for firstLetter, entriesInGroup in entriesByGroup %}
   {# your alphabetical entry listing %}
{% endfor %}

I do something similar here on the username_1 Archive page but with a slightly different approach (here's a video on it).

> **Comments:**
> **username_0**: Thanks so much for the help Ryan (assuming it's Ryan ). I wasn't familiar with the `keys` filter. That did what I needed. Will definitely watch the video you linked to.  
> **username_1**: It's me! You're welcome. :)  

---

