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

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

问题描述

JavaScript 中是否有任何类型的not in"运算符来检查对象中是否不存在属性?我在 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 语句中.设置 ifelse 语句只是为了使用 else 部分对我来说似乎是错误的.

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.

推荐答案

为了使用 else 部分而设置 if/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天全站免登陆