用于检查对象属性的Javascript“not in”运算符

时间:2021-08-25 04:18:39

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 SO. Here's a small snippet of code I'm working on where I need this kind of functionality:

JavaScript中是否有任何“not in”运算符来检查对象中是否存在属性?我无法在Google或SO周围找到任何相关信息。这是我正在处理的一小段代码,我需要这种功能:

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...

如你所见,我将把所有内容都放在else语句中。我只是为了使用else部分来设置if / else语句似乎是错的...

3 个解决方案

#1


220  

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

我只是为了使用else部分来设置if / else语句似乎是错的...

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

只是否定你的条件,你将在if中获得else逻辑:

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

#2


23  

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

正如Jordão所说,只是否定它:

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.

注意:上述测试是否tutorTimes具有在id中指定名称的属性,原型链中的任何位置。例如,tutorTimes中的“valueOf”返回true,因为它是在Object.prototype中定义的。

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

如果要测试当前对象中是否存在属性,请使用hasOwnProperty:

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

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

或者,如果您有一个hasOwnPropery键,您可以使用:

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

#3


10  

Two quick possibilities:

两种快速可能性:

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

or

要么

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

#1


220  

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

我只是为了使用else部分来设置if / else语句似乎是错的...

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

只是否定你的条件,你将在if中获得else逻辑:

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

#2


23  

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

正如Jordão所说,只是否定它:

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.

注意:上述测试是否tutorTimes具有在id中指定名称的属性,原型链中的任何位置。例如,tutorTimes中的“valueOf”返回true,因为它是在Object.prototype中定义的。

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

如果要测试当前对象中是否存在属性,请使用hasOwnProperty:

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

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

或者,如果您有一个hasOwnPropery键,您可以使用:

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

#3


10  

Two quick possibilities:

两种快速可能性:

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

or

要么

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