I have json based data structure with objects containing nested objects. In order to access a particular data element I have been chaining references to object properties together. For example:
var a = b.c.d;
If b or b.c is undefined, this will fail with an error. However, I want to get a value if it exists otherwise just undefined. What is the best way to do this without having to check that every value in the chain exists?
I would like to keep this method as general as possible so I don't have to add huge numbers of helper methods like:
var a = b.getD();
or
var a = helpers.getDFromB(b);
I also want to try to avoid try/catch constructs as this isn't an error so using try/catch seems misplaced. Is that reasonable?
Any ideas?