mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.71k stars 846 forks source link

Issue with using for loop #625

Closed jkalandarov closed 2 years ago

jkalandarov commented 3 years ago

Hi,

I have an expected issue with rendering while using for loop. The data object topCityWeather is sent from the back-end to the front-end. While rendering data on the browser I have a funny issue: for loop is iterating as twice as expected.

//The length of the topCityWeather is 10 items <% for (var i = 1; i <= topCityWeather.length; i++) { %> <tr> <td><%= i %></td> <td><%= topCityWeather[i].city %></td> <td><%= topCityWeather[i].temperature + '℃' %></td> <td><%= topCityWeather[i].humidity + '%' %></td> <td><%= topCityWeather[i].description %></td> </tr> <% } %>

Снимок

webdpro commented 2 years ago

Probably already figured this one out, but I would guess you have some blank entries in your object

jkalandarov commented 2 years ago

Yeah, figured it out a few days later. Kinda embarrassing now :)

webdpro commented 2 years ago

Nothing to be embarrassed about :D

Only know because I have been there, and there will be some poor soul who will have the same issue and come across this post and be like 'This just saved my day!'