小提琴這裏:下劃線_each。功能越來越嵌套對象循環
在for循環下劃線模板中: <% for(var item in thing.items) { %>
我想獲得item.name,但<%= item.name %>
不輸出任何東西。
如何從每個things.items
對象獲取屬性?謝謝!
我的JSON數據是這樣的:
var things = [{
"name": "Chair",
"title": "Chairs",
"items": [{
"name": "Recliner",
"title": "Recliner Chair",
"type": "Chair",
"quantity": "1"
},
{
"name": "Club/Armchair",
"title": "Club/Armchair",
"type": "Chair",
"quantity": 1
}]
},
{
"name": "Table",
"title": "Tables",
"items": [{
"name": "End Table",
"title": "End Table",
"type": "Table",
"quantity": "1"
},
{
"name": "Coffee Table",
"title": "Coffee Table",
"type": "Table",
"quantity": 1
}]
}];
而且我的模板看起來像這樣:
<script type="text/html" id='furniture-template'>
<% _.each(things,function(thing,key,list){
// create more variables
%>
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" href="#things-<%= thing.name %>">
<%= thing.title %>
</a>
</div> <!-- header -->
<div id="things-<%= thing.name %>" class="accordion-body collapse in">
<div class="accordion-inner">
<% for(var item in thing.items) { %>
<div class="item">
<a class="item-add" data-type="Chairs" data-name="Recliner"><%= item.name %></a>
</div>
<% } %>
</div> <!-- inner -->
</div> <!-- accordion-body -->
<% }); %>
</script>