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