Underscore template display array of objects via _.each
11,316
Assuming comments is an array on your model:
<% _.each(comments, function(comment) { %>
<p><%= comment.comment %></p>
<% }); %>
Related videos on Youtube
Author by
ObviousCat
Updated on September 15, 2022Comments
-
ObviousCat over 1 year
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