我有这个:
field: [
{ text: 'text 1' },
{ price1: '10' },
{ price2: '16' },
{ text: 'text2' },
{ price1: '20' },
{ price2: '23' }
];
我想用 mustache 重复数据,我已经尝试过:
{{#field}}
<span>{{text}}</span>
{{/field}}
但是不起作用,有人可以帮忙吗?
最佳答案
当迭代 Mustache 中的 JSON 时,您可以执行以下操作
{{#each field}}
<span> {{ text }} </span>
{{/each}}
顺便说一句,你的 JSON 没有正确呈现,我认为每个 JSON Objcet 都必须看起来像这样
field: [
{
text: 'text 1,
price1: '10',
price2: '16'
},
{
text: 'text2',
price1: '20',
price2: '23'
},
];
这样你就可以访问对象中的所有元素
关于javascript - 使用 Mustache 循环 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52574566/