<p>For the hash key to be dynamic, you must wrap it in parentheses:</p>

<pre><code>(i): {
</code></pre>

CC BY-SA 4.0
2020-01-14T17:46:59.627Z
Awesome, it works! Thanks Brandon
Jonathan Schofield
CC BY-SA 4.0
2020-01-14T17:44:00.307Z
2020-01-14T17:44:00.307Z
Brandon Kelly
<p><strong>1:</strong> I'm creating a <code>data</code> hash object and then looping through a collection of entries to store the results of various macros in it: </p>

<pre><code>{% set data = {} %}
{% set people = craft.entries().section('people').all() %}
</code></pre>

<p><strong>2:</strong> I'm setting a reference to each entry and then merging the content compiled into my <code>data</code> object: </p>

<pre><code>{% for person in people %}
    {% set i = loop.index %}

    {# get and set key|values #}

     {% set data = data|merge({
        i: {
            "key": value, 
            "otherKey":  otherValue
        }
    }) %}
{% endfor %}
</code></pre>

<p><strong>3:</strong> And then I'm looping through my <code>data</code> object for output:</p>

<pre><code>{% for row in data %}
    &lt;tr&gt;
        &lt;td&gt;{{ row.key|raw }}&lt;/td&gt;
        &lt;td&gt;{{ row.otherKey|raw }}&lt;/td&gt;
    &lt;/tr&gt;
{% endfor %}
</code></pre>

<p>The structure of the output at stage 3 is fine, but it's only resulting in 1 <code>row</code> out of 45 <code>people</code> entries.</p>

<p>What am I doing wrong at 2 please?</p>

<merge><hash>
Problem with data hash only storing last entry in loop
# Problem with data hash only storing last entry in loop

(Asked by: username_0 on 2020-01-14)

1: I'm creating a data hash object and then looping through a collection of entries to store the results of various macros in it: 
{% set data = {} %}
{% set people = craft.entries().section('people').all() %}

2: I'm setting a reference to each entry and then merging the content compiled into my data object: 
{% for person in people %}
    {% set i = loop.index %}

    {# get and set key|values #}

     {% set data = data|merge({
        i: {
            "key": value, 
            "otherKey":  otherValue
        }
    }) %}
{% endfor %}

3: And then I'm looping through my data object for output:
{% for row in data %}
    
        {{ row.key|raw }}
        {{ row.otherKey|raw }}
    
{% endfor %}

The structure of the output at stage 3 is fine, but it's only resulting in 1 row out of 45 people entries.
What am I doing wrong at 2 please?

**Tags:** merge, hash


---

## 1 answers

### Answer by username_1 on 2020-01-14. Score: 2

For the hash key to be dynamic, you must wrap it in parentheses:
(i): {

> **Comments:**
> **username_0**: Awesome, it works! Thanks Brandon  

---

