Does anyone know a clean way to find out if an object property exists? Here is my example:
var test = {
a : 'west',
b : {
a : 'rest'
},
d : {
a : 'pest'
}
};
// I want to access 'a' in 'c'
typeof test.c.a; // fails!
typeof
it seems can't get past the fact that 'c'
doesn't exist to check if 'a'
exists inside it (I've also tried jQuery.type()
which also fails in the same way - I would have thought it would have error checking inside that function).
In this example of course I could simply check if 'c'
exists first but in my real situation I have a large and deep object and I need to dynamically retrieve data from any potential location so it would be nice if there were a ready-made solution which didn't necessitate having to use a try-catch
.
Thanks in advance!