[英]How do i find if an object contains an array in javascript?
I have a huge set of data which is basically an array of objects. 我有大量的数据,基本上是对象数组。 I need to loop through all the objects to see if any contain any array themselves.
我需要遍历所有对象以查看是否包含数组本身。
Thoughts ? 有什么想法吗?
Try this: 尝试这个:
for (var i = 0; i < arr.length; i++) {
var obj = arr[i];
for (var j in obj) {
if (!obj.hasOwnProperty(j)) {
continue;
}
if (obj[j] instanceof Array) {
console.log(i + '.' + j + ' is array');
}
}
}
You can use filter
to grab only objects that match a condition and then check if any of the values is an array with some
. 您可以使用
filter
仅捕获符合条件的对象,然后检查是否有任何值是带有some
的数组。
Example: 例:
var arr = [
{ x: 1 },
{ x: 2, a: [] },
{ x: 3, a: [] },
{ x: 1 },
];
arr.filter(a => Object.values(a).some(o => o instanceof Array))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.