Jørgen Jørgen - 7 months ago 37
Javascript Question

How can i get object key in a json-file with handlebars

in the following json file:

{
"students":[
{
"timestamp": "1,45198E+12",
"personnummer": 1234567891011,
"fornavn": "Some name"
}
]
}


I'm using handlebars to make a table out of this information, how can i go about geting "timestamp","personnummer" and so forth in to the tag.
I have tried :
Index:
{{@index}} Value = {{this}}

and: Key:
{{@key}} Value = {{this}}

But i just get: "Index: 0 Value = [object Object]", is can someone shed som light on this ?

Answer

If

var obj = {
  "students":[
  {
    "timestamp": "1,45198E+12",
    "personnummer": 1234567891011,
    "fornavn": "Some name"
  }
 ]
}

then your handlebar should look like a loop:

   <ul class="student_list">
  {{#each students.[0]}}
    <li>{{@key}}</li>
  {{/each}}
</ul>

http://jsfiddle.net/vduqnhsb/

Comments