Skip to content

Instantly share code, notes, and snippets.

@connrs
Created March 24, 2011 16:07
Show Gist options
  • Save connrs/885314 to your computer and use it in GitHub Desktop.
Save connrs/885314 to your computer and use it in GitHub Desktop.
Create multidimensional object from array of keys and a val
var x = {key:['x','y','z'],val:123},
c = x.key.length,
b = false,
memo = {};
for(var y=0;y<c;y++) {
if (!b && !memo[x.key[y]]) {
memo[x.key[y]] = y<c-1 ? {} : x.val;
b = memo[x.key[y]];
} else if (!b[x.key[y]]) {
b[x.key[y]] = y<c-1 ? {} : x.val;
b = b[x.key[y]];
} else {
b[x.key[y]] = y<c-1 ? {} : x.val;
b = b[x.key[y]];
}
}
console.log(memo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment