在JavaScript中是否有任何类型的“not in”操作符来检查对象中是否存在属性?我在谷歌或Stack Overflow周围找不到任何关于这个的东西。下面是我正在做的一小段代码,我需要这种功能:

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语句似乎是错误的。


当前回答

您可以将条件设置为false

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

其他回答

两个简单的可能性:

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

or

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

对我来说,设置一个if/else语句只是为了使用else部分似乎是错误的…

只要对你的条件求反,你就会得到if语句中的else逻辑:

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

if(!tutorTimes[id]){./*do xx */..}

您可以将条件设置为false

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

我个人认为

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

更容易阅读

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

但两者都可以。