Underscore _each. function getting nested object in loop

Fiddle here:

http://jsfiddle.net/rBhfx/79/

In this for loop within an underscore template: <% for(var item in thing.items) { %>

I wanted to get item.name, but the <%= item.name %> is not outputting anything.

How do I get the properties from each things.items object? Thanks!

my JSON data looks like this:

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
        }]
}];

And my template looks like this:

<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>

Answers


Have you tried using:

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

Instead of:

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

Need Your Help

Asserting attributes on a capybara object

attributes find selector capybara assert

I am currently working with capybara for integration testing and as part of our testing framework. I am running into an issue that I cannot seem to find a solution to. So the scenario is this, I

How to get a win32 handle of an open file in python?

python winapi

I'm sure this is documented somewhere but i can't find it...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.