json 检查对象数组是否包含特定键

6ojccjat  于 2022-11-19  发布在  其他
关注(0)|答案(6)|浏览(212)

我需要确定某个键是否存在于对象数组中。
下面是一个示例数组:

arrOfObj = [{
        "mainKey1": {
            "subKey1": {
                "innerKey1": {
                    "innerMostKey1": {
                        "key1": "value"
                    }
                }
            }
        }
    }, {
        "mainKey2": {
            "key2": "value"
        }
    }, {
        "mainKey3": {
            "subKey3": {
                "key3": "value"
            }
        }
    }
]

我尝试这样做,但得到错误的输出:

const objKeys = Object.keys(arrOfObj)
console.log('objKeys = ' + JSON.stringify(arrOfObj))

输出为索引号:

objKeys = ["0", "1", "2"]

我想有一个这样的函数:

var isKeyPresent = checkKeyPresenceInArray('mainKey3')

请注意,我只需要检查对象中的最顶层--在上面的例子中,这些是主键(mainKey1等),它们的内容是动态的(有些对象内部嵌套很深,有些则不是)。
救命啊!

eufgjt7s

eufgjt7s1#

您可以尝试使用array.some()

let checkKeyPresenceInArray = key => arrOfObj.some(obj => Object.keys(obj).includes(key));
let arrOfObj = [{
        "mainKey1": {
            "subKey1": {
                "innerKey1": {
                    "innerMostKey1": {
                        "key1": "value"
                    }
                }
            }
        }
    }, {
        "mainKey2": {
            "key2": "value"
        }
    }, {
        "mainKey3": {
            "subKey3": {
                "key3": "value"
            }
        }
    }
]

let checkKeyPresenceInArray = key => arrOfObj.some(obj => Object.keys(obj).includes(key));


var isKeyPresent = checkKeyPresenceInArray('mainKey3')

console.log(isKeyPresent);
ssgvzors

ssgvzors2#

你可以遍历数组,检查是否有对象有你要找的键,如果有就返回true。如果没有找到键,for循环就会结束,返回false。

arrOfObj = [{
        "mainKey1": {
            "subKey1": {
                "innerKey1": {
                    "innerMostKey1": {
                        "key1": "value"
                    }
                }
            }
        }
    }, {
        "mainKey2": {
            "key2": "value"
        }
    }, {
        "mainKey3": {
            "subKey3": {
                "key3": "value"
            }
        }
    }
]

function arrayHasKey(arr, key) {
  for (const obj of arr) {
    if (key in obj) { return true; }
  }
  return false;
}

console.log(arrayHasKey(arrOfObj, "mainKey2"))
console.log(arrayHasKey(arrOfObj, "mainKey10"))
mnemlml8

mnemlml83#

这将工作,它返回布尔值:

arrOfObj.hasOwnProperty('mainKey3');
mklgxw1f

mklgxw1f4#

您可以将somehasOwnProperty搭配使用,如下所示:

let checkKeyPresenceInArray = (key) => arrOfObj.some((o) => o.hasOwnProperty(key));
j7dteeu8

j7dteeu85#

您必须使用**hasOwnProperty**方法来检查该数组中的对象中是否存在该键-

var c = 0;
arrOfObj.forEach(e => {
  if(e.hasOwnProperty("mainKey1")){
       c++;
   }
});
if(c > 0 && c == arrOfObj.length){
    console.log("The key is found in all the objects in the array.");
}
else if(c == 0){
    console.log("The key is not found in any objects in the array");
}
else{
     console.log("The key is  found in some objects in the array");
}
7vhp5slm

7vhp5slm6#

为此,可以应用JSON.stringify()

let list = [{
        "data1": {
            "subKey1": {
                "innerKey1": {
                    "innerMostKey1": {
                        "key1": "value"
                    }
                }
            }
        }
    }, {
        "data2": {
            "key2": "value"
        }
    }, {
        "data3": {
            "subKey3": {
                "key3": "value"
            }
        }
    }
]

let checkIfInArray = key => list.some(obj => JSON.stringify(obj).indexOf(key) > -1);

var result = checkIfInArray('key2')

alert(result);

相关问题