handlebars - how to access first element of child array?

handlebars.js, html, javascript

Solution

In order to get the first element, you would need:

{{#each object.parts}}
    {{this.[0]}}
{{/each}}

but this would just print [object object].

The second requirement - viewing it as JSON - requires a helper in your JS:

Handlebars.registerHelper('json', function(context) {
    return JSON.stringify(context);
});

and then:

{{#each object.parts}}
    {{json this.[0]}}
{{/each}}

Problem

I have an data object, which contains an array within an array, I want to loop over the parent array and read out the first object of each child array. In the example I want to read out: {"id":1}, {"id":9}, {"id":11} ``` var object = { parts: [ [{"id":1},{"id":2},{"id":3}], [{"id":9},...], [{"id":11},... ] ] } ``` so far I have a for each loop: ``` {{#each object.parts}} ... {{/each}} ```

Original source