简体   繁体   English

如何查找对象是否包含JavaScript中的数组?

[英]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.

相关问题 如何在JavaScript数组中按对象查找对象 - How do I find an object by object inside a javascript array 如何找到Javascript中包含对象或数组的JSON对象? - How to find JSON object contains Object or Array in Javascript? 如何在 javascript 数组中找到对象值? - how do I find an object value in a javascript array? 如何在 Javascript 的数组中返回仅包含某个 object 的对象数组? - How do I return an array of objects that only contains a certain object in an array for Javascript? Javascript:如何检查数组是否包含另一个数组? - Javascript: How do I check if an array contains exactly another array? 如何将包含数组的json转换为javascript对象? - How can I convert a json that contains array to javascript object? 我有一个对象数组。 每个 object 都包含最大和最小键值对。 如何找到包含预定义值的 object? - I have an array of objects. Each object contains max and min key value pairs. How do I find the object that contains a predefined value? 如何在数组 object 中找到元素并更新 Javascript 中的源数组 - How do I find element in array object and update source array in Javascript 我怎么知道一个数组在Javascript中包含全零(0) - How do I know an array contains all zero(0) in Javascript 如果对象包含具有空数组的键,如何删除该对象? - How do I remove an object if it contains a key with an empty array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM