初始化 ejs 变量后,我无法更改值并且增量运算符不起作用。
这是我的代码:
<% results1.forEach(function(result1, index){ %>
<% results2.forEach(function(result2, index1){ %>
<% if(result1.id==result2.parent)
{
var i=1; %>
<tr>
<td><%= index+1 %>.<%= i %></td>
<td><%= result2.title %></td>
<td><%= result1.title %></td>
<td align="center"><%= result2.views %></td>
</tr>
<% i++;
} %>
<% }); %>
<% }); %>
在上面的代码中我声明了一个变量 i 并在底部添加了 i++。
最佳答案
i
应在 forEach
语句之外声明,否则在每个 cicle 处重新声明
修改上面的代码
<% results1.forEach(function(result1, index){ %>
<% var i=1; results2.forEach(function(result2, index1){ %>
<% if(result1.id==result2.parent)
{
<tr>
<td><%= index+1 %>.<%= i %></td>
<td><%= result2.title %></td>
<td><%= result1.title %></td>
<td align="center"><%= result2.views %></td>
</tr>
<% i++;
} %>
<% }); %>
<% }); %>
关于node.js - ejs 变量增量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50775713/