admin管理员组文章数量:1289933
I have this data structure:
flavors": {
"sour": 0.16666666666666666,
"salty": 0.16666666666666666,
"sweet": 0,
"meaty": 0.16666666666666666,
"bitter": 0.16666666666666666
}
My html is this:
<p>Flavors:</p>
<ul>
{{#Flavors}}
<li>{{Flavours.Name}}</li> // doesn't work //
{{/Flavors}}
</ul>
What I'm trying to do is get at the name of the flavour: i.e. salty, sour, etc. I want to be able to cater for arbitrary values is the JSON, and not code them in the html block.
I have this data structure:
flavors": {
"sour": 0.16666666666666666,
"salty": 0.16666666666666666,
"sweet": 0,
"meaty": 0.16666666666666666,
"bitter": 0.16666666666666666
}
My html is this:
<p>Flavors:</p>
<ul>
{{#Flavors}}
<li>{{Flavours.Name}}</li> // doesn't work //
{{/Flavors}}
</ul>
What I'm trying to do is get at the name of the flavour: i.e. salty, sour, etc. I want to be able to cater for arbitrary values is the JSON, and not code them in the html block.
Share Improve this question asked Dec 11, 2013 at 11:41 mikemike 1011 silver badge6 bronze badges 04 Answers
Reset to default 12You may iterate over the object in this way:
{{#each myObject}}
Key: {{@key}} Value = {{this}}
{{/each}}
For details check this post: Handlebars/Mustache - Is there a built in way to loop through the properties of an object?
the answer to my question is on this page: Handlebars/Mustache - Is there a built in way to loop through the properties of an object?
it's Ben's answer. Works very well indeed!
Iterate over json and use key to get property names.
for (var key in obj) {
alert(' name=' + key + ' value=' + obj[key]);
// do some stuff here
}
Your template should be like the following:
<p>Flavors:</p>
<ul>
{{#each flavours}}
<li>{{@key}}</li>
{{/each}}
</ul>
according with the documentation on the website.
本文标签: javascriptHow can I get the names of namevalue pairs in HandlebarsStack Overflow
版权声明:本文标题:javascript - How can I get the names of name-value pairs in Handlebars? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741444666a2379123.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论