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
部分而设置if
-else
语句是错误的。
发布于 2019-04-11 16:23:38
我个人认为
if (id in tutorTimes === false) { ... }
更容易阅读
if (!(id in tutorTimes)) { ... }
但这两种方法都会起作用。
发布于 2011-11-02 04:31:06
两种快速的可能性:
if(!('foo' in myObj)) { ... }
或
if(myObj['foo'] === undefined) { ... }
发布于 2021-09-06 10:48:41
您可以将条件设置为false
if ((id in tutorTimes === false)) { ... }
https://stackoverflow.com/questions/7972446
复制相似问题