Nicole M
Nicole M

Reputation: 13

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. The way I would prefer is pure 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.

Any ideas?

Upvotes: 0

Views: 2570

Answers (5)

Emeeus
Emeeus

Reputation: 5250

In this case, I think one of the most efficient way is to do a for and break like:

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. I think this is also the case of .some()

Upvotes: 0

Henok Tesfaye
Henok Tesfaye

Reputation: 9560

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"));

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138267

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))

Upvotes: 1

Eduardo Escobar
Eduardo Escobar

Reputation: 3389

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");
}

Upvotes: 0

Hodrobond
Hodrobond

Reputation: 1696

You're trying to filter an array of objects. You can pass a custom function into Array.prototype.filter, defining a custom search function. 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'));

Upvotes: 0

Related Questions