Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 419
dylanized Avatar asked Apr 22 '13 17:04

dylanized


People also ask

What are templates in EJS?

EJS (Embedded JavaScript Templating) is one of the most popular template engines for JavaScript. As the name suggests, it lets us embed JavaScript code in a template language that is then used to generate HTML.

What is EJS and express?

Template Engines and EJS:Template engine is a part of Express that enables us to use static files in our applications. Template engine converts variables to values and changes the template to HTML files to send to the client. The default template engine of Express is Jade, but EJS is the most commonly used engine.


1 Answers

You can use forEach method

myArray.forEach(function(el, index) {     // el - current element, i - index }); 
like image 92
wachme Avatar answered Sep 24 '22 13:09

wachme