user3848987
user3848987

Reputation: 1657

Meteor: Iterate over nested elements

UPDATE

I updated the question:

I have got this result of a collection, which I want to use in my template. So I iterate with the each-spacebar. The problem is, that I want to get all article-datasets and complete the output with the journal-field.

{
    "journal" : "journalSingle",
    "article" : [
        {
            "title" : "title1",
            "edition" : "test",
            "reference" : "4NoHjACkjHJ8mavv9"
        }
    ]
}
{
    "journal" : "multiple",
    "article" : [
        {
            "title" : "title2",
            "edition" : "test",
            "reference" : "4NoHjACkjHJ8mavv9"
        },
        {
            "title" : "title3",
            "edition" : "test",
            "reference" : "4NoHjACkjHJ8mavv9"
        }
    ]
}

template

{{#each item}}
    <div>
        <input data-field="journal" type="text" value="{{journal}}">
        <input data-field="edition" type="text" value="{{article.title}}">
    </div>
{{/each}}

The result should be for that example:

<div>
    <input data-field="journal" type="text" value="journalSingle">
    <input data-field="edition" type="text" value="title1">
</div>
<div>
    <input data-field="journal" type="text" value="multiple">
    <input data-field="edition" type="text" value="title2">
</div>
<div>
    <input data-field="journal" type="text" value="multiple">
    <input data-field="edition" type="text" value="title3">
</div>

Upvotes: 2

Views: 62

Answers (1)

Keith Nicholas
Keith Nicholas

Reputation: 44316

because article is an array you can't go article.title.

 {{#each item}}
    <input data-field="journal" type="text" value="{{journal}}">
    {{#each article}}
         <input data-field="edition" type="text" value="{{title}}">
    {{/each}}
   {{/each}}

you can do this, but it is weird you can edit journal in multiple places

 {{#each item}}

    {{#each article}}
       <div>
         <input data-field="journal" type="text" value="{{..journal}}">
         <input data-field="edition" type="text" value="{{title}}">
       </div>
    {{/each}}
   {{/each}}

Upvotes: 1

Related Questions