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

↘锁芯ラ 提交于 2019-11-29 11:02:19

问题


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


回答1:


You can use forEach method

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


来源:https://stackoverflow.com/questions/16153384/inside-express-ejs-templates-what-is-cleanest-way-to-loop-through-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!