ejs变量增量不起作用

问题描述 投票:1回答:1

初始化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 ++。

node.js express ejs
1个回答
0
投票

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++; 
                        } %>
          <% }); %>
    <% }); %>
© www.soinside.com 2019 - 2024. All rights reserved.