Check if object properties exist

var pets = { 
  "cats": { 
    "persian": {
      "black": 1,
      "white": 2,
    }, 
    "sphynx": {
      "blue": 2,
      "white": 2,
    }
  },
  "dogs": {
    "pug": {
      "black": 1,
      "brown": 2,
      "white": 1,
    },
    "bulldog": {
      "brown": 1,
      "white": 1,
    }
  }
}
if(pets.persian.black){
  //This might throw an exception if persian does not exist. Error: black of undefined
}

 

Solution with lodash:

_.get(pets, ‘persian.black’)