JavaScript中是否有一个"not in"运算符来检查对象属性? [英] Is there a “not in” operator in JavaScript for checking object properties?

查看:118
本文介绍了JavaScript中是否有一个"not in"运算符来检查对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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部分来设置ifelse语句对我来说似乎是错误的.

As you can see, I’d be putting everything into the else statement. It seems wrong to me to set up an ifelse 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆