Let's say I have an object:
var foo = {'bar1': {'baz1':1}};
And I try to access foo['bar2']['baz2']
. If I was just trying to access foo['bar2']
, JS would return undefined
. But because I'm trying to get the next property after the undefined bar2
, JS throws TypeError: Cannot read property 'baz2' of undefined.
Is there some automatic accessor for an object that first checks if baz2
exists in foo
before trying to call its properties? I know I could use a try/catch block or an if statement, but I was hoping for a function along the lines of C++'s array::at
, that would check for me.