4

I have JSON array as follows:

{outer:[
    {
      Key1:"ID001", 
      Key2:[
             {
               innerKey1:"Myval1",
               innerKey2:"Myvalue2"
             }
            ]
    }
]}

Now in my HTML file :

<div>
    {{#each outer}}
        <b>key1:</b> {{this.Key1}} <br/>
        {{#each this.Key2}}
           <b>InnerKey1:</b> {{this.innerKey1}} <br/>
        {{/each}}
    {{/each}}
</div>

But it not showing up any inner values. Could anyone please help how to loop on inner array objects (i.e Key2 above). Do I need to write a separate helper for this?

3
  • What about {{#outer}}? Commented Aug 16, 2013 at 13:02
  • @BenjaminGruenbaum Can you please explain what exactly you meant by {{#outer}}? Commented Aug 16, 2013 at 13:28
  • {{#outer}} is an iterator, it iterates through the elements in outer. Commented Aug 16, 2013 at 13:29

1 Answer 1

9

You must choose between object or array interation:

{
  outer:[{
    Key1:"ID001", 
    Key2:{
      innerKey1:"Myvalue1",
      innerKey2:"Myvalue2"
    },
    Key3:[
      "Myvalue4",
      "Myvalue5"
    ]    
  }]
}

<div>
  {{#each outer}}
    key1: {{this.Key1}} <br/>
    {{#each this.Key2}}
      {{@key}}: {{this}}
    {{/each}} <br/>
    {{#each this.Key3}}
      {{@index}}: {{this}}
    {{/each}}
  {{/each}}
</div> 

outputs:

key1: ID001 
innerKey1: Myvalue1 innerKey2: Myvalue2 
0: Myvalue4 1: Myvalue5

http://codepen.io/rafaelcastrocouto/pen/qgrFE

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.