-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

3

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.