프로그래밍/자바스크립트

자바스크립트 - EJS 형식에서 forEach문 의미 및 사용법

가카리 2015. 1. 6. 16:13
반응형

Question.

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

 

Answer.

You can use forEach method.

forEach 문을 써보세요.

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

 

 출처 : http://stackoverflow.com/questions/16153384/inside-express-ejs-templates-what-is-cleanest-way-to-loop-through-an-array

반응형