You could create your own accesser function:
function access(obj, path) {
var arr = path.split('/');
while(obj && arr.length)
obj = obj[arr.shift()];
return obj;
}
And use it like this:
var bank = {
customerlist: {customer: [{address: 'foo'}]}
}
access(bank, 'customerlist/customer/0/address'); // 'foo'
access(bank, 'bar/foo/foobar'); // undefined (no error)
Also consider using...
function access(obj, path) {
var arr = path.split('/');
while(obj!=null && arr.length)
obj = obj[arr.shift()];
return obj;
}
...if you want to use access
with non-objects, e.g. you want access('', 'length')
to return 0
Explaining,
function access(obj, path) {
var arr = path.split('/');
while (
obj /* To avoid `null` and `undefined`. Also consider `obj != null` */
&& /* Logical AND */
arr.length /* Check that `arr` still has elements */
) {
obj = obj[arr.shift()]; /* `arr.shift()` extracts the first
element is `arr` */
}
return obj;
}