JavaScript中是否有一个"not in"运算符来检查对象属性? [英] Is there a “not in” operator in JavaScript for checking object properties?
问题描述
JavaScript中是否存在某种不存在"运算符来检查对象中是否不存在属性?我在Google或Stack Overflow上找不到任何相关信息.这是我正在研究需要这种功能的一小段代码:
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
}
});
如您所见,我会将所有内容放入else
语句中.对于我来说,仅仅使用else
部分来设置if
– else
语句对我来说似乎是错误的.
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.
推荐答案
设置if/else语句仅使用else部分对我来说似乎是错误的...
It seems wrong to me to set up an if/else statement just to use the else portion...
只需取消条件,您就会在if
中获得else
逻辑:
Just negate your condition, and you'll get the else
logic inside the if
:
if (!(id in tutorTimes)) { ... }
这篇关于JavaScript中是否有一个"not in"运算符来检查对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!