Created
March 24, 2011 16:07
-
-
Save connrs/885314 to your computer and use it in GitHub Desktop.
Create multidimensional object from array of keys and a val
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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