how can I declare a value deep in an object tree using variable properties in javascript

Posted by joshs on Stack Overflow See other posts from Stack Overflow or by joshs
Published on 2010-03-23T02:49:43Z Indexed on 2010/03/23 2:51 UTC
Read the original article Hit count: 236

Filed under:

I am trying to have a javascript object tree behave like a php associative array in the following way.

var key1 = 'a';
var key2 = 'b';
var key3 = 'c';

var obj[key1][key2][key3] = 'd';

However, in javascript I believe you need to define each property/object pair individually, forming deeper leaves. Something like:

var obj[key1] = {};
var obj[key1][key2] = {};
...

Is there a way to simplify or shorten this script?

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript