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() }}

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]) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.