mardi 24 juillet 2018

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?

Aucun commentaire:

Enregistrer un commentaire