Javascript 在每个下面划线。函数获取循环中的嵌套对象

Javascript 在每个下面划线。函数获取循环中的嵌套对象,javascript,jquery,json,backbone.js,underscore.js,Javascript,Jquery,Json,Backbone.js,Underscore.js,在这里拉小提琴: 在此下划线模板内的for循环中: 我想获取item.name,但是没有输出任何内容 如何从每个事物.items对象获取属性?谢谢 我的JSON数据如下所示: var things = [{ "name": "Chair", "title": "Chairs", "items": [{ "name": "Recliner", "title": "Recliner Chair", "typ

在这里拉小提琴:

在此下划线模板内的for循环中:

我想获取item.name,但是
没有输出任何内容

如何从每个
事物.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>
我的模板如下所示:

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>

成功了

您是否尝试过使用:

<% _.each(thing.items, function(item) { %>
    …
<% }); %>
而不是:

<% for(var item in thing.items) { %>
    …
<% } %>