最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to check if some field does not exist in JavaScript - Stack Overflow

programmeradmin0浏览0评论

I have jQuery code like

      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.

I have jQuery code like

      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.

Share Improve this question edited Dec 17, 2021 at 23:52 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Nov 5, 2017 at 9:59 NidaNida 1,7023 gold badges36 silver badges75 bronze badges 3
  • Instead of the empty character, check for undefined, and use strict parison operator. – Teemu Commented Nov 5, 2017 at 10:04
  • can u show me in the code – Nida Commented Nov 5, 2017 at 10:08
  • This is pure javascript, theres no jquery involved here. – Jonas Wilms Commented Nov 5, 2017 at 10:29
Add a ment  | 

4 Answers 4

Reset to default 2

Try this:

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

You can also try with JQuery like:

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

Just check if its undefined:

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

Your code is paring your variable to an empty string. But if this variable is not defined, it can’t pare it, so you can do :

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

If the « status » var is defined but just empty you can do

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

You check like this

if(!indivudvalbookdetails[j]['status'])
发布评论

评论列表(0)

  1. 暂无评论