i'm having some trouble in finding a way to check if in a parsed json object is present a property. For example in my js i have this line of code:
jsonArray = JSON.parse(jsonResponse)
I wanna check if in the jsonArray object there is the property media
.
For example if my json look like this one:
Object0 {hashtags: Array[0],
symbols: Array[0],
user_mentions: Array[1],
urls: Array[0]}
Object1 {hashtags: Array[1],
symbols: Array[0],
user_mentions: Array[0],
urls: Array[1],
media: Array[1]}
i wanna check if Object0
has property media
and if Object1
has property media
.
Thank's
You can use hasOwnProperty
:
if (Object0.hasOwnProperty('media')) {
// Object0.media
}
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property. Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With