有没有一个“不在”? JavaScript 中的运算符用于检查对象属性?

发布于 2024-12-13 04:56:05 字数 427 浏览 2 评论 0原文

JavaScript 中是否有任何类型的“not in”运算符来检查对象中是否不存在属性?我在 Google 或 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 部分而设置 ifelse 语句似乎是错误的。

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
  }
});

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

十秒萌定你 2024-12-20 04:56:05

在我看来,仅仅为了使用 else 部分而设置 if/else 语句是错误的...

只需否定您的条件,您就会在 ifelse 逻辑代码>:

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

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

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

if (!(id in tutorTimes)) { ... }
亢潮 2024-12-20 04:56:05

就我个人而言,我发现

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

比这更容易阅读

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

,但两者都可以工作。

Personally I find

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

easier to read than

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

but both will work.

笑忘罢 2024-12-20 04:56:05

正如Jordão已经说过的,直接否定它:

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

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

如果您想测试当前对象中是否不存在某个属性,请使用 hasOwnProperty:

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

或者如果您可能有一个 hasOwnPropery 密钥,您可以使用此:

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

如果您的环境支持 ECMA-292 2022 年 7 月,您可以使用 Object.prototype.hasOwnProperty 的便捷替代方案 Object.hasOwn

if (!Object.hasOwn(tutorTimes,id)) { ... }

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

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.

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

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

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

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

If your environment supports ECMA-292 from July 2022, you can use the convenient alternative to Object.prototype.hasOwnProperty Object.hasOwn:

if (!Object.hasOwn(tutorTimes,id)) { ... }
萌逼全场 2024-12-20 04:56:05

两种快速的可能性:

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

或者

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

Two quick possibilities:

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

or

if(myObj['foo'] === undefined) { ... }
我们只是彼此的过ke 2024-12-20 04:56:05

你可以将条件设​​置为 false

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

you can set the condition to be false

if ((id in tutorTimes === false)) { ... }
感情废物 2024-12-20 04:56:05

可读性不太好,但一个快速的简写可能是:

if (id in tutorTimes ^ 1) { ... }

当使用 XOR 进行运算时,falsetrue 被转换为 01分别。因此结果相反。

not very readable but a quick short hand could be:

if (id in tutorTimes ^ 1) { ... }

when doing operation with XOR, false and true got converted into 0 and 1 respectively. hence that reverse the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文