-3

I have an array of object with this structure

[
  {
    "Apple": "fruit"
  },
  {
    "Orange": "fruit"
  },
  {
    "Cake": "sweet"
  }
]

How i can check Apple is present in this array of object using javascript

Mark
  • 90,562
  • 7
  • 108
  • 148
Warrior
  • 5,168
  • 12
  • 60
  • 87
  • 1
    `[].find()` will help you – Sirko Jan 16 '20 at 05:28
  • Does this answer your question? [How to determine whether an object has a given property in JavaScript](https://stackoverflow.com/questions/1894792/how-to-determine-whether-an-object-has-a-given-property-in-javascript) – Tân Jan 16 '20 at 05:31
  • @Tân Not a dup, OP has an array of objects. – Teemu Jan 16 '20 at 05:39

1 Answers1

1

You can use some() and test with in or Object.hasOwnProperty:

let arr = [ { "Apple": "fruit" }, { "Orange": "fruit" }, { "Cake": "sweet" } ] 

console.log(arr.some(obj => obj.hasOwnProperty('Apple')))
console.log(arr.some(obj => obj.hasOwnProperty('Bannana')))

This will return true for the first condition that matches and false if none match.

Mark
  • 90,562
  • 7
  • 108
  • 148