我尝试记录除了用户,_id等之外的数组中的每个名称。执行if(!word === "users")
工作并按照我的预期记录用户条目。我可能会忽略一些微不足道的事情,并提前道歉。谢谢。
let arr = ["noun","nounoffensive","nounvulgar","adjective","verb","verbinformal","conjunction","exclamation","users","_id","word","createdAt","updatedAt","__v"]
arr.forEach((word)=>{
if(!word === "users" || "_id" || "word" || "createdAt" || "updatedAt"){
console.log(word)
};
});
答案 0 :(得分:3)
您的if
声明不正确。您缺少word ===
进行其他比较,!
应该存在整个表达式。
let arr = ["noun","nounoffensive","nounvulgar","adjective","verb","verbinformal","conjunction","exclamation","users","_id","word","createdAt","updatedAt","__v"]
arr.forEach((word)=>{
if(!(word === "users" || word === "_id" || word === "word" || word === "createdAt" || word === "updatedAt")){
console.log(word)
};
});

替代方法也可以是创建一个数组,例如let notInArray = ["users", "_id", "word", "createdAt", "updatedAt"];
,其中包含您要排除的单词:
let arr = ["noun","nounoffensive","nounvulgar","adjective","verb","verbinformal","conjunction","exclamation","users","_id","word","createdAt","updatedAt","__v"]
let notInArray = ["users", "_id", "word", "createdAt", "updatedAt"];
arr.forEach((word)=>{
if(notInArray.indexOf(word) === -1){
console.log(word)
};
});

答案 1 :(得分:1)
你不能使用
if(!word === "users" || "_id" || "word" || "createdAt" || "updatedAt")
好像是陈述。
您可以使用数组来检查它是否在里面:
let arr = ["noun","nounoffensive","nounvulgar","adjective","verb","verbinformal","conjunction","exclamation","users","_id","word","createdAt","updatedAt","__v"]
const words2find = ['users', '_id', 'word', 'createdAt', 'updatedAt'];
arr.forEach((word)=>{
if(words2find.indexOf(word) < 0){
console.log(word)
};
});