简体   繁体   English

如何检查具有多个对象的数组中的值是否存在-JavaScript?

[英]How can I check if a value exists in an array with multiple objects - javascript?

So my array looks like this: 所以我的数组看起来像这样:

let array = [
    {"object1":1},
    {"object2":2},
    {"object3":3}
];

What I want to do is to check, for example, whether or not "object1" exists. 我想要做的是例如检查“ object1”是否存在。 The way I would prefer is pure Javascript. 我更喜欢纯Javascript。

I am doing this for large chunks of data and so my code needs to be something like this: 我正在对大量数据进行此操作,因此我的代码需要是这样的:

if ("opensprint1" in array){
  console.log("yes, this is in the array");
} else {
  console.log("no, this is not in the array");
};

NOTE: I have tried to use the (in) function in JS and the (hasOwnProperty) and neither has worked. 注意:我试图在JS和(hasOwnProperty)中使用(in)函数,但都没有用。

Any ideas? 有任何想法吗?

if ("opensprint1" in array){

That check for the array keys, so it would work with: 该检查数组键,因此它可以与:

if ("0" in array){

But actually you want to check if some of the array elements got that key: 但是实际上您想检查某些数组元素是否具有该键:

if(array.some( el => "opensprint1" in el))

You're trying to filter an array of objects. 您正在尝试过滤对象数组。 You can pass a custom function into Array.prototype.filter , defining a custom search function. 您可以将自定义函数传递给Array.prototype.filter ,从而定义一个自定义搜索函数。 It looks like you want to search based on the existence of keys. 看起来您想根据键的存在进行搜索。 If anything is returned, that key exists in the object array. 如果返回任何内容,则该键存在于对象数组中。

 let array = [{ "object1": 1 }, { "object2": 2 }, { "object3": 3 } ]; const filterByKey = (arr, keyName) => array.filter(obj => Object.keys(obj).includes(keyName)).length > 0; console.log(filterByKey(array, 'object1')); console.log(filterByKey(array, 'object5')); 

That is roughly equivalent to: 大致相当于:

 let array = [{ "object1": 1 }, { "object2": 2 }, { "object3": 3 } ]; const filterByKey = (arr, keyName) => { // iterate each item in the array for (let i = 0; i < arr.length; i++) { const objectKeys = Object.keys(arr[i]); // take the keys of the object for (let j = 0; j < objectKeys.length; j++) { // see if any key matches our expected if(objectKeys[i] === keyName) return true } } // none did return false; } console.log(filterByKey(array, 'object1')); console.log(filterByKey(array, 'object5')); 

This might help you 这可能对您有帮助

let array = [
    {"object1":1},
    {"object2":2},
    {"object3":3}
];

let targetkey = "opensprint1";
let exists  = -1;
for(let i = 0; i < array.length; i++) {
    let objKeys = Object.keys(array[i]);
    exists = objKeys.indexOf(targetkey);
    if (exists >= 0) {
        break;
    }
}

if (exists >= 0) {
    console.log("yes, this is in the array");
} else {
   console.log("no, this is not in the array");
}

In this case, I think one of the most efficient way is to do a for and break like: 在这种情况下,我认为最有效的方法之一是进行for and break

 let array = [ {"object1":1}, {"object2":2}, {"object3":3} ]; exist = false; for(let i = 0; i<array.length; i++){ if("object1" in array[i]){ exist = true;//<-- We just know the answer we want break;//<-- then stop the loop } } console.log(exist); 

When iteration finds a true case, stops the iteration. 当迭代找到正确的情况时,停止迭代。 We can't perform a break in .map , .filter etc. So the number of iterations are the less possible. 我们无法在.map.filter等中执行break 。因此,迭代次数越少。 I think this is also the case of .some() 我认为.some()也是这样

let array = [
 { "object1": 1 },
 { "object2": 2 },
 { "object3": 3 }
];

let checkKey = (key) => {
 var found = false;
 array.forEach((obj) => {
     if (!(obj[key] === undefined)) {
         found = true;
         array.length = 0;
     }
 });
 return found;
}
console.log(checkKey("object2"));

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

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