I'm working on a dynamic structure, as a basic example:
var myObj = {
"fruits" : {
"apples" : {
"color" : "red",
"price" : "$0.49"
}
},
"colors" : {}
};
So I'm anticipating that for example the fruit, "orange" does not exist. I want to test that it doesn't exist, so I can add it.
I see in console that you are told "undefined" if you try to access an "object" that doesn't exist.
That's what I'm looking for but I don't want the error in the console, I want to know/anticipate that it's empty so I can add to it.
If I do something like
console.log(myObj.fruits.apples.color);
I'd get red, so if I did that to orange
console.log(myObj.fruits.orange.color);
it would say undefined, at least if I tried
console.log(myObj.fruits.apples.weight);
That should be undefinied, my jsfiddle is not doing so hot, I can't reproduce that undefined console.log message at the moment
Okay, so it seems that provided the object exists, you can test for an undefined property like the weight example above.
I'm not looking for that, I'm looking to test if an object like "coconut" doesn't exist, so I can add it to the object array using extend. But I don't want the red console log error, I want to be able to anticipate it using a comparison like:
if (myObj.fruits.coconut === "undefined") {
// means it doesn't exist, add it
}
I realize with my own test, that would produce the error, not the undefined like the apple weight console log demo above