node.js - ejs变量增量不起作用
问题描述
初始化 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++;
} %>
<% }); %>
<% }); %>
推荐阅读
- php - PayPal IPN 循环遍历 PHP 中的购物车项目,字符串文字问题
- c# - 在 VS2010 中安装 Microsoft.Data.Analysis
- vbscript - 注册表项中的无效根 (VBScript)
- reactjs - Next.js 静态页面 - json 数据
- docker - 已关闭 - docker 容器上的 Ansible 任务 - 在 PATH 中找不到 Docker 命令
- python - 对半正弦距离的 gps 数据进行分组并计算平均值
- objective-c - 锁定原理——在Objective C中
- flutter - 如何在flutter中从网络图像创建pdf
- keras - 根据论文实现EEGNet卷积神经网络的问题
- php - 在更新 Laravel 时从空值创建默认对象