我遇到了麻烦。我想将来自两个数组的内容相互比较。如果它们是===我想运行if语句,否则会出现其他语句。到目前为止,这起作用了,但是IF和其他情况不仅发生。
<% for (var i = 0; i < match.interests.length; i++) { %>
<% for (var j = 0; j < user.interests.length; j++) { %>
<% if (match.interests[i] === user.interests[j]) { %>
<li class="tag positive"><%= match.interests[i] %></li>
<% } else {%>
<li class="tag"><%= match.interests[i] %></li>
<% } %>
<% } %>
<% } %>
您的平等检查很好,问题之所以出现,是因为您总是在其他语句中记录某些内容。
console.clear();
const match = {
interests: [
'Code',
'JS'
],
};
const user = {
interests: [
'Code',
'Apples',
'Skiing'
],
};
const output = [];
for (let i = 0; i < match.interests.length; i++) {
for (let j = 0; j < user.interests.length; j++) {
console.log('loop')
if (match.interests[i] === user.interests[j]) {
console.log('MATCH');
output.push(match.interests[i]);
} else {
console.log('DOESN'T MATCH');
output.push(match.interests[i]);
}
}
}
console.log(output);
请参阅以下代码的输出,您需要确定所需的输出,并相应地调整循环