28

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

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

但我想知道,有没有更清洁的方法来做到这一点?

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

4

1 回答 1

70

你可以使用forEach方法

myArray.forEach(function(el, index) {
    // el - current element, i - index
});
于 2013-04-22T18:24:59.140 回答