@wachme's answer is correct, but to stick with the original question, here is the version using Underscore's _.each
(and integrated in template syntax):
<% _.each(myArray, function(el, index) { %>
Value is <%= el %>, index is <%= index %>.
<% }) %>
The advantage of Array.prototype.forEach
is that you don't need to depend on Underscore in your templates. The advantage of _.each
is that it has some additional tricks up its sleeves (for example, it also works on objects) and that it works in older JS environments without any need for polyfills.
As an aside, Underscore's _.template
can be used instead of EJS, although it has fewer features. Also, the meaning of <%=
and <%−
is swapped between the two libraries. Naturally, you can always use _.each
in Underscore templates.