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 if
–else
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 if
–else
statement just to use the else
portion.
6 Answers
Reset to default 565It 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
版权声明:本文标题:Is there a “not in” operator in JavaScript for checking object properties? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736721677a1949501.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var id = ...
in your function. – Cobby Commented Nov 20, 2012 at 2:20a !in b
... – myol Commented Jul 17, 2024 at 7:49