我需要访问句柄模板内 JSON 数组中的键名称。我的 JSON 数组如下所示。
{
"attributes":[
{
"name":"malintha"
},
{
"age":"29"
},
{
"marks":"81"
}
]
}
以下是我的 Handlebars 模板。我需要访问键值以及相关键的值。
{{#attributes}}
<{{#key}}>{{#value}}</{{#key}}>
{{/attributes}}
预期输出为
<name>malintha</name>
<age>29</age>
<marks>81</marks>
但是我没有得到预期的输出。我怎样才能做到这一点?
最佳答案
您必须使用#each
函数,并迭代您的 JSON 数组。
然后,您必须使用相同的函数迭代对象中的每个属性,并使用 @key
访问属性名称:
{{#each attributes}}
{{#each this}}
<{{@key}}>{{this}}</{{@key}}>
{{/each}}
{{/each}}
关于javascript - 访问 JSON 数组中的键名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51365571/