本文介绍了Javascript“不在”中用于检查对象属性的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

var tutorTimes = {};
$(checked).each(function(idx) {
    id = $(this).attr('class');
    if(id in tutorTimes) {

    }
    else {
        //Rest of my logic will go here
    }
});

正如你所看到的,我会将所有内容都放入else语句中。设置if / else语句只是为了使用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...

推荐答案

否定你的情况,你将在中得到 else 逻辑如果

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

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

这篇关于Javascript“不在”中用于检查对象属性的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:35