繁体   English   中英

如何访问另一个对象内部某个对象上的元素?

[英]How to access to an element that is on an object inside another object?

这是“大”数组,其中包含两个对象:

bigArray : [Object, Object]
          0:Object
            id:"1"
            text:"t1"
          1:Object
            id:"2"
            text:"t2"

这是console.log(bigArray)返回的内容。

我的问题是:如何获取两个元素t1和t2来验证其中之一是否未定义(其中一个还是两个都未定义)?

使用Array.prototype.some()

bigArray.some(x=> typeof x === "undefined")

您可以遍历要获取的数组,并以以下方式检查字段:

var bigArray = [
          {

            id:"1",
            text:"t1"
          },
          {
            id:"2",
            text:"t2"
          }
         ];

            for ( var i = 0; i< bigArray.length; i++ ) {
                alert(bigArray[i].id);
              // this field will be the field agains which you need to check
              if ( typeof bigArray[i].somefield == "undefined" ) {
                alert("its undefined");
              }
            }

这是小提琴: https : //jsfiddle.net/swaprks/sjmd06rm/1/

暂无
暂无

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

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