Inside Express / EJS模板,什么是最简单的方法来循环数组? [英] Inside Express/EJS templates, what is cleanest way to loop through an array?

查看:154
本文介绍了Inside Express / EJS模板,什么是最简单的方法来循环数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <%for(var i = 0; i< myArray。 length; i ++){
this = myArray [i];
//显示此
}%>的属性

但我想知道,是否有更干净的方法来做到这一点?



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

解决方案

您可以使用 forEach 方法

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


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

解决方案

You can use forEach method

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

这篇关于Inside Express / EJS模板,什么是最简单的方法来循环数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆