【问题标题】:checking an object for null element检查对象的空元素
【发布时间】:2015-02-20 08:07:09
【问题描述】:

http GET 方法的响应如下所示:

{
  id:1,
  name:"John",
  subjects:[],
  totalMarks:458
}

在前端我想检查subjects 属性是否为空。 我已经尝试过这种方法但不起作用

var newObj= {
    id:1,
    name:"John",
    subjects:[],
    totalMarks:458
}

if (newObj.subjects == null) {
  alert("Empty subjects");
}

【问题讨论】:

标签: javascript arrays arraylist arrayobject


【解决方案1】:

newObj.subjectsArray,所以你需要像这样检查它

if(Array.isArray(newObj.subjects) && !newObj.subjects.length) {
   alert("Empty subjects");
}

【讨论】:

    【解决方案2】:

    您可以使用length 属性来检查数组是否为空..

     if(newObj.subjects.length==0)
        {
           alert("Empty subjects");
        }
    

    【讨论】:

      猜你喜欢
      • 2013-06-14
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      • 1970-01-01
      相关资源
      最近更新 更多