3

I'm having trouble printing out a simple for each comment loop with _.template.

<% _.each([comments], function(i) { %>  <p><%= i %></p> <% }); %>

prints [object Object]

<% _.each([comments], function(i) { %>  <p><%= JSON.stringify(i) %></p> <% }); %>

prints:

[{"comment":"Mauris quis leo at diam molestie sagittis.","id":263,"observation_id":25}]

What I've tried so far:

<% _.each([comments], function(i) { %>  <p><%= i.comment %></p> <% }); %>

blank

<% _.each([comments], function(i) { %>  <p><%= i.get('comment') %></p> <% }); %>

Uncaught TypeError: Object [object Array] has no method 'get'

<% _.each([comments], function(i) { %>  <p><%= comment %></p> <% }); %>

blank

2
  • And what does comments contains? That's basically what's important here. Commented Apr 17, 2013 at 21:45
  • And why are you wrapping comments in an array? I would assume that it's already an array, right? Commented Apr 17, 2013 at 22:04

1 Answer 1

15

Assuming comments is an array on your model:

<% _.each(comments, function(comment) { %>  
  <p><%= comment.comment %></p> 
<% }); %>
Sign up to request clarification or add additional context in comments.

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.