We have a large hierarchical object (worst possible legacy design) in javascript. The problem I am facing is that there is a list of null checks I need to perform whenever I want to access an element deep within the object structure.
Say I have a bank object which contains a list of customers and I want to get the address of the first customer,
if(bank != null ||
bank.customerlist != null ||
bank.customerlist.customer[0] != null ||
bank.customerlist.customer[0].address != null )
{
transactionAddress = bank.customerlist.customer[0].address;
}
This is just a small example,I cannot believe so many null checks are required just to access a single value.
It there a better way around this?
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;
}