跳过 if 语句和循环中的未定义值
Skip undefined values in if statement and loop
我试图遍历所有用户的评论,但使用 if 语句查找特定值。问题是我的应用程序中断,因为一些用户没有发表评论,因此我得到一个“无法读取 'collected' 的 属性 未定义。如何跳过 if 语句的未定义值?代码如下:
<% for(var i=0; i < users.length; i++) { %>
<tr>
<% if(users[i].comments.slice(-1)[0].collected !== 'At Reception') { %>
<td>Nothing in reception - well done!</td>
<% } else { %>
<td><%= users[i].studio %></td>
<td><%= users[i].name %></td>
<td><%= users[i].email %></td>
<td><%= users[i].username %></td>
<td><%= users[i].comments.slice(-1)[0].collected %></td>
<td><a class="btn btn-primary" href="/users/<%= users[i]._id %>">More Info</a></td>
<% } %>
你试过了吗continue;
?我知道你可以把它放在某个地方的块中。如果它不符合您的条件标准,它将跳过并迭代到下一个 comment/list。
只需添加检查以查看对象是否存在,以及评论长度是否 > 0:
<% for(var i=0; i < users.length; i++) { %>
<tr>
<% if(!users[i].comments || users[i].comments.length == 0) {
continue;
} else if(users[i].comments && users[i].comments.length > 0 && users[i].comments.slice(-1)[0].collected !== 'At Reception') { %>
<td>Nothing in reception - well done!</td>
<% } else { %>
<% console.log('nothing in reception') %>
<td><%= users[i].studio %></td>
<td><%= users[i].name %></td>
<td><%= users[i].email %></td>
<td><%= users[i].username %></td>
<td><%= users[i].comments.slice(-1)[0].collected %></td>
<td><a class="btn btn-primary" href="/users/<%= users[i]._id %>">More Info</a></td>
<% } %>
首先使用这行代码检查 属性 是否可用于该对象:
yourObject.hasOwnProperty('collected') // it will return true if exist, false if not.
if(yourObject.hasOwnProperty('collected')) {
// code here...
} else {
// catch here...
}
的更多信息
我试图遍历所有用户的评论,但使用 if 语句查找特定值。问题是我的应用程序中断,因为一些用户没有发表评论,因此我得到一个“无法读取 'collected' 的 属性 未定义。如何跳过 if 语句的未定义值?代码如下:
<% for(var i=0; i < users.length; i++) { %>
<tr>
<% if(users[i].comments.slice(-1)[0].collected !== 'At Reception') { %>
<td>Nothing in reception - well done!</td>
<% } else { %>
<td><%= users[i].studio %></td>
<td><%= users[i].name %></td>
<td><%= users[i].email %></td>
<td><%= users[i].username %></td>
<td><%= users[i].comments.slice(-1)[0].collected %></td>
<td><a class="btn btn-primary" href="/users/<%= users[i]._id %>">More Info</a></td>
<% } %>
你试过了吗continue;
?我知道你可以把它放在某个地方的块中。如果它不符合您的条件标准,它将跳过并迭代到下一个 comment/list。
只需添加检查以查看对象是否存在,以及评论长度是否 > 0:
<% for(var i=0; i < users.length; i++) { %>
<tr>
<% if(!users[i].comments || users[i].comments.length == 0) {
continue;
} else if(users[i].comments && users[i].comments.length > 0 && users[i].comments.slice(-1)[0].collected !== 'At Reception') { %>
<td>Nothing in reception - well done!</td>
<% } else { %>
<% console.log('nothing in reception') %>
<td><%= users[i].studio %></td>
<td><%= users[i].name %></td>
<td><%= users[i].email %></td>
<td><%= users[i].username %></td>
<td><%= users[i].comments.slice(-1)[0].collected %></td>
<td><a class="btn btn-primary" href="/users/<%= users[i]._id %>">More Info</a></td>
<% } %>
首先使用这行代码检查 属性 是否可用于该对象:
yourObject.hasOwnProperty('collected') // it will return true if exist, false if not.
if(yourObject.hasOwnProperty('collected')) {
// code here...
} else {
// catch here...
}
的更多信息