Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-22T15:58:18.663Z
Answers.LastActivityDate: 2016-03-22T15:58:18.663Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <templating><array><merge>
Title: Create an object of objects with merge
ThreadText: # 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]) %}
  
  ```
  
  
  ---
  
  