简体   繁体   English

如何检查JavaScript中是否不存在某些字段

[英]How to check if some field does not exist in javascript

I have jquery code like 我有类似的jQuery代码

      for(var j in indivudvalbookdetails) {
         if(indivudvalbookdetails[j]['status'] == "") {
          ... 
         }
     }

Now there might be some items in the loop where status field won't exists, if there are such items the code in the if condition should work. 现在,循环中可能会有一些状态字段不存在的项目,如果有这些项目,则if条件中的代码应该起作用。

Please help!!! 请帮忙!!!

Just check if its undefined: 只需检查其是否未定义即可:

if(typeof indivudvalbookdetails[j]['status'] === "undefined") { ... if(typeof indivudvalbookdetails [j] ['status'] ===“ undefined”){...

Your code is comparing your variable to an empty string. 您的代码正在将变量与空字符串进行比较。 But if this variable is not defined, it can't compare it, so you can do : 但是,如果未定义此变量,则无法对其进行比较,因此可以执行以下操作:

if(indivudvalbookdetails[j]['status'] == undefined)

If the « status » var is defined but just empty you can do 如果定义了«status»var但为空,则可以执行

 if(indivudvalbookdetails[j]['status'] == null)

你这样检查

if(!indivudvalbookdetails[j]['status'])

Try this: 尝试这个:

var myElement =  indivudvalbookdetails[j]['status'];
if (typeof(myElement) != 'undefined' && myElement != null)
{
  // exists.
}

You can also try with JQuery like: 您也可以尝试使用JQuery,例如:

if ($('#elementId').length > 0) {
  // exists.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM