<p>You're overriding the "title" and "link" properties on every iteration. Wrap the <code>itemObject</code> object in <code>[]</code> to merge the whole object into your array.</p>

<pre><code>{% set object = object|merge([itemObject]) %}
</code></pre>

CC BY-SA 3.0
2016-03-22T15:58:18.663Z
2016-03-22T15:58:18.663Z
carlcs
<p>I'm trying to create an object of objects, can anyone suggest why <code>object</code> would only contain the last iteration of "itemObject"?</p>

<pre><code>{% set object = {} %}

{% for entry in craft.entries.section("mainSectionsStructure") %}
    {% set itemObject = {
            "title": entry.title,
            "link": entry.url
        } %}

    {% set object = object|merge(itemObject) %}

{% endfor %}

{{ object|json_encode() }}
</code></pre>

<templating><array><merge>
Create an object of objects with merge
# Create an object of objects with merge

(Asked by: username_0 on 2016-03-22)

I'm trying to create an object of objects, can anyone suggest why object would only contain the last iteration of "itemObject"?
{% set object = {} %}

{% for entry in craft.entries.section("mainSectionsStructure") %}
    {% set itemObject = {
            "title": entry.title,
            "link": entry.url
        } %}

    {% set object = object|merge(itemObject) %}

{% endfor %}

{{ object|json_encode() }}

**Tags:** templating, array, merge

---

## 1 answers

### Answer by username_1 on 2016-03-22. Score: 3

You're overriding the "title" and "link" properties on every iteration. Wrap the itemObject object in [] to merge the whole object into your array.
{% set object = object|merge([itemObject]) %}


---

