gpt4 book ai didi

json - Mustache.js 循环遍历 JSON 数据

转载 作者:行者123 更新时间:2023-12-03 20:22:12 27 4
gpt4 key购买 nike

谁能建议我的模板循环应该如何在以下示例中循环遍历 JSON 数据?
演示在这里:http://jsfiddle.net/Seefeld/LbVEH/

 {
"0": {
"Dosage": "25",
"Drug": "Indocin",
"Patient": "David",
"Date": "15/11/2012 14:29:14"
},
"1": {
"Dosage": "50",
"Drug": "Enebrel",
"Patient": "Sam",
"Date": "15/11/2012 14:29:14"
},
"2": {
"Dosage": "10",
"Drug": "Hydralazine",
"Patient": "Christoff",
"Date": "15/11/2012 14:29:14"
},
"3": {
"Dosage": "21",
"Drug": "Combivent",
"Patient": "Janet",
"Date": "15/11/2012 14:29:14"
},
"4": {
"Dosage": "100",
"Drug": "Dilantin",
"Patient": "Melanie",
"Date": "15/11/2012 14:29:14"
}
}

我在 mustache.js 上看到的所有示例假设您知道对象名称。
任何建议非常感谢。

最佳答案

您可以转换 data变成一个真正的数组(无论如何都应该是):

var drugs = [];
for (var i = 0, drug; (drug = data[i]); ++i) {
drugs.push(drug);
}

var template = "{{#drugs}}<p>{{Drug}}</p>{{/drugs}}";
var html = Mustache.to_html(template, {drugs: drugs});
$(html).appendTo("#cnt");

或自己浏览数据:
var template = "<p>{{Drug}}</p>";
for (var i = 0, drug; (drug = data[i]); ++i) {
var html = Mustache.to_html(template, drug);
$(html).appendTo("#cnt");
}

关于json - Mustache.js 循环遍历 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13399816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com