[英]How to access to an element that is on an object inside another object?
Here is the "big" array that contains two objects here: 这是“大”数组,其中包含两个对象:
bigArray : [Object, Object]
0:Object
id:"1"
text:"t1"
1:Object
id:"2"
text:"t2"
This what console.log(bigArray)
returns. 这是console.log(bigArray)
返回的内容。
My question is: How to get the two elements t1 and t2 to verify if one of them is undefined (one of them or both)? 我的问题是:如何获取两个元素t1和t2来验证其中之一是否未定义(其中一个还是两个都未定义)?
bigArray.some(x=> typeof x === "undefined")
You can iterate over the array which you are getting and check for the field in the following manner: 您可以遍历要获取的数组,并以以下方式检查字段:
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");
}
}
Here is the fiddle: https://jsfiddle.net/swaprks/sjmd06rm/1/ 这是小提琴: https : //jsfiddle.net/swaprks/sjmd06rm/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.