1

I'm trying to use underscore.js templating to iterate over an object

var list = "<% _.each(resgistrations, function(resgistration) { %> <tr><td><%= i %></td><td><%= resgistration %></td></tr> <% }, i); %>";

I don't understand why i won't work as my iterator. I get a reference error i is not defined. This seems like it's straight out of the docs for _.each

1
  • 1
    You are not creating the index parameter; of course i is undefined. Commented Nov 26, 2013 at 20:47

2 Answers 2

3

You are missing the second parameter, the iterator i. Try this:

var list = "<% _.each(resgistrations, function(resgistration, i) { %> <%= i %><%= resgistration %> <% }); %>";
Sign up to request clarification or add additional context in comments.

Comments

0

You specified the first parameter as registration for the iterator value, and didn't assign a second parameter for the index — or rather, you passed it in the wrong place. The fixed code would be:

var list = "<% _.each(resgistrations, function(resgistration, i) { %> <tr><td><%= i %></td><td><%= resgistration %></td></tr> <% }); %>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.