To check without receiving an error:
this snippet allows you to check if a chained object exists.
var x; try{x=MyArray[name1][name2][name3][name4]}catch(e){} !x||(x.push('whatever'));
of
fooobar.com/questions/1682 / ...
Reducing javascript object chaining
this function allows you to create chains with a simple string.
function def(a,b,c,d){ c=b.split('.'); d=c.shift();//add *1 for arrays a[d]||(a[d]={});//[] for arrays !(c.length>0)||def(a[d],c.join('.')); }
Using
var MyArray={};//[] def(MyArray,'name1.name2.name3.name4');//name1+'.'+name2....
of
fooobar.com/questions/964207 / ...
both work also for arrays with simple change.replace {} with []
If you have questions, just ask.
cocco source share