我有一个带有字段的 json 数据。我想在 mustache js 中渲染它。但问题是列表中的一项是动态生成的。我怎样才能迭代这个项目。
我使用了给定的代码
'{{#view}}{{#.}}<td class="number">{{.}}</td>{{/.}}{{/view}}' +
'{{#comment}}{{#.}}<td class="number">{{.}}</td>{{/.}}{{/comment}}' +
'{{#rating}}{{#.}}<td class="number">{{.}}</td>{{/.}}{{/rating}}' +
但它呈现为[object object]
我该如何解决这个问题?
最佳答案
.
仅用于遍历字符串数组,因此您的模板将不起作用。但是,您始终可以在将数据解析为模板之前处理/修改数据:
for (key in data){
if (key == 'comment' || key == 'rating' || key == 'view'){
//Turn it into an array
var temp_array = [];
for (key2 in data[key]){
// To make sure it is not a function or __proto__ object whatsoever
if (typeof data[key][key2] != "object" && typeof data[key][key2] != "function") {
temp_array.push(data[key][key2]);
}
}
data[key] = temp_array;
}
}
把item列表改成数组再解析到模板。
希望这对您有所帮助。
关于javascript - 如何在 mustache 中呈现动态生成的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25073877/