1

I'm trying to log each name in the array except for the users, _id, etc. Doing if(!word === "users") works and logs the user entry as I would expect. I'm probably overlooking something trivial and apologize in advance. Thank you.

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)
    };
});
Scott Enock
  • 71
  • 11

2 Answers2

3

Your if statement is incorrect. You are missing word === for other comparison and ! should be there for entire expression.

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)
    };
});

The alternative approach could also be to create a array, say, let notInArray = ["users", "_id", "word", "createdAt", "updatedAt"]; that contains the word you want to exclude:

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)
  };
});
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
1

You cannot use

if(!word === "users" || "_id" || "word" || "createdAt" || "updatedAt")

as if statement.

You could use an array to check if it is inside:

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)
    };
});
Danmoreng
  • 2,367
  • 1
  • 19
  • 32
  • You should be more precise if you don't want to mislead future readers. We can totally use things like this as an if statement. It's valid javascript code. But it's just not what the OP thought it would do. By the way this test is always `true` – Kaddath May 18 '18 at 14:27
  • I̶ ̶w̶o̶u̶l̶d̶n̶'̶t̶ ̶s̶a̶y̶ ̶t̶h̶i̶s̶ ̶i̶s̶ ̶v̶a̶l̶i̶d̶ - nvm, I see what you're saying. – Lewis May 18 '18 at 14:33
  • yes i know we both know ;) but a beginner could be puzzled here, nothing really important though – Kaddath May 18 '18 at 14:39