admin管理员组文章数量:1317904
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}}
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}}
Share
Improve this question
asked Feb 21, 2014 at 17:10
user3260177user3260177
1132 silver badges7 bronze badges
1 Answer
Reset to default 8In 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}}
本文标签: javascripthandlebarshow to access first element of child arrayStack Overflow
版权声明:本文标题:javascript - handlebars - how to access first element of child array? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742031119a2416460.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论