How can i check if a child attribute of a json object is null? While firefox recognizes it, Chrome and IE 8 don’t.
I have a json object like this:
centralData.phone.id;
centralData.address.id;
centralData.product.id;
//and many others
And i’d like to check if some of it’s attributes might be null. I’m doing this and it works:
if(centralData.phone != null){
//Do things
}
But this don’t, since it’s not always i have a StockGroup
if(centralData.product.StockGroup != null){
//Error
}
So, how can i check if centralData.product.StockGroup is null?
You don’t want to check if it’s
null, you want to check if the property exists (it would beundefinedin this case). Your check works because you are using==instead of===, which converts between types (undefined == null, butundefined !== null).When you want to check for a nested property, you need to check every level. I would recommend to use the
inoperator, as it checks if the property exists and ignores it’s value.This does what you want to do: