我有以下JSON:
{ "Food" : [ { "Meat" : "beef lamb turkey",
"Vegetables" : "broccoli potatoes carrots",
"Fruit" : "apples bananas strawberries",
"Sugar" : "candy softdrinks",
"Dairy" : "milk cheese yoghurt",
"Grains" : "wheat barley rice",
"Fat" : "fries KFC",
},
]
我正在尝试循环使用此功能,并仅显示" Sugar"和"胖"项目
我有这个:
<script type="text/html" id="badFoods">
<div>
<% _.each(container.Food, function(food, i) {%>
display the name here i.e Sugar and Fat
</div>
<% }); %>
</div>
</script>
当我运行它时,我希望它显示以下内容:
糖:糖果软饮料 脂肪:炸薯条肯德基 道歉,这可能有点模糊,只是想要朝着正确的方向努力,这样我才能理解这一点。谢谢!
答案 0 :(得分:0)
#1
<% _.each(container.Food, function(food, key) {%>
Sugar: <%= food.Sugar %> Fat: <%= food.Fat %>
<% }); %>
#2
<% _.each(container.Food, function(food, key) { %>
<% _.each(food, function(value, key) { %>
<% if (key === 'Sugar' || key === 'Fat') { %>
<%= key %>: <%= value %>
<% } %>
<% }); %>
<% }); %>