I'm aware that api team is the one responsible for sending correct data to the client that requested the data. However, I still would like to know the best way of checking if property exists or not.
// when state property is missing from the api response
myObject = {
name : 'Scott',
addressInfo : {
address1 : '444 St Peter St',
address2 : 'Apartment D',
zipCode : '55555'
},
birthDate : '20000101'
}
or
// or when birtdate is missing
myObject = {
name : 'Scott',
addressInfo : {
address1 : '444 St Peter St',
address2 : 'Apartment D',
zipCode : '55555',
state : 'MN'
}
}
or
// when addressInfo is missing
myObject = {
name : 'Scott',
birthDate : '20000101'
}
Is the code below enough for the checking?
if (myObject.addressInfo !== undefined && myObject.addressInfo.state !== undefined) {
// console.log(
}