I'm trying to use an if (key in object)
statement to get if a key exists in an object, however I've noticed that it only works for the first level of an object, like this:
myObject = {
name: 'Bob',
age: 20,
dog: true
}
However, it doesn't seem to work with nested objects like this:
myObject = {
name: 'Joe',
favorites: {
ice_cream: 'vanilla',
coffee: 'espresso'
}
}
If I were to run console.log('coffee' in myObject)
, it would return false. I've also tried using myObject.hasOwnProperty('coffee')
, however it still returns false.