node.js 在 Express/EJS 模板中,循环数组的最简洁方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16153384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:16:58  来源:igfitidea点击:

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

node.jsexpressunderscore.jsejspartials

提问by dylanized

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

我有一个使用 EJS 模板设置的 Express.js 应用程序。我用经典的 JS 语法成功地循环了一个数组:

<% 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

具体来说,我可以使用 Underscore 或 Lodash 来循环遍历 .each 吗?谢谢你

回答by wachme

You can use forEachmethod

你可以使用forEach方法

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