Inside Express/EJS templates, what is cleanest way to loop through an array?

dylanized picture dylanized · Apr 22, 2013 · Viewed 26.8k times · Source

I have an Express.js app set up using EJS templates. I successfully looped through an array with classic JS syntax:

<% for (var i = 0; i < myArray.length; i++) { 
    this = myArray[i];
    // display properties of this
} %>

But I'm wondering, is there a cleaner way to do this?

Specifically, can I use Underscore or Lodash to loop through with .each ? thank you

Answer

wachme picture wachme · Apr 22, 2013

You can use forEach method

myArray.forEach(function(el, index) {
    // el - current element, i - index
});