admin管理员组

文章数量:1129706

Is there any sort of "not in" operator in JavaScript to check if a property does not exist in an object? I couldn’t find anything about this around Google or Stack Overflow. Here’s a small snippet of code I’m working on where I need this kind of functionality:

var tutorTimes = {};

$(checked).each(function(idx){
  id = $(this).attr('class');

  if(id in tutorTimes){}
  else{
    //Rest of my logic will go here
  }
});

As you can see, I’d be putting everything into the else statement. It seems wrong to me to set up an ifelse statement just to use the else portion.

Is there any sort of "not in" operator in JavaScript to check if a property does not exist in an object? I couldn’t find anything about this around Google or Stack Overflow. Here’s a small snippet of code I’m working on where I need this kind of functionality:

var tutorTimes = {};

$(checked).each(function(idx){
  id = $(this).attr('class');

  if(id in tutorTimes){}
  else{
    //Rest of my logic will go here
  }
});

As you can see, I’d be putting everything into the else statement. It seems wrong to me to set up an ifelse statement just to use the else portion.

Share Improve this question edited Aug 17, 2019 at 18:40 Sebastian Simon 19.5k8 gold badges60 silver badges84 bronze badges asked Nov 1, 2011 at 20:25 AaronAaron 10.8k13 gold badges40 silver badges53 bronze badges 2
  • 5 I think you might want var id = ... in your function. – Cobby Commented Nov 20, 2012 at 2:20
  • 1 Waiting for someone to implement a !in b... – myol Commented Jul 17, 2024 at 7:49
Add a comment  | 

6 Answers 6

Reset to default 565

It seems wrong to me to set up an if/else statement just to use the else portion...

Just negate your condition, and you'll get the else logic inside the if:

if (!(id in tutorTimes)) { ... }

Personally I find

if (id in tutorTimes === false) { ... }

easier to read than

if (!(id in tutorTimes)) { ... }

but both will work.

As already said by Jordão, just negate it:

if (!(id in tutorTimes)) { ... }

Note: The above test if tutorTimes has a property with the name specified in id, anywhere in the prototype chain. For example "valueOf" in tutorTimes returns true because it is defined in Object.prototype.

If you want to test if a property doesn't exist in the current object, use hasOwnProperty:

if (!tutorTimes.hasOwnProperty(id)) { ... }

Or if you might have a key that is hasOwnPropery you can use this:

if (!Object.prototype.hasOwnProperty.call(tutorTimes,id)) { ... }

If your environment supports ECMA-292 from July 2022, you can use the convenient alternative to Object.prototype.hasOwnProperty Object.hasOwn:

if (!Object.hasOwn(tutorTimes,id)) { ... }

Two quick possibilities:

if(!('foo' in myObj)) { ... }

or

if(myObj['foo'] === undefined) { ... }

you can set the condition to be false

if ((id in tutorTimes === false)) { ... }

not very readable but a quick short hand could be:

if (id in tutorTimes ^ 1) { ... }

when doing operation with XOR, false and true got converted into 0 and 1 respectively. hence that reverse the result.

本文标签: Is there a “not in” operator in JavaScript for checking object propertiesStack Overflow