Skip to content

Instantly share code, notes, and snippets.

@eivindingebrigtsen
Created April 25, 2012 09:22
Show Gist options
  • Save eivindingebrigtsen/2488440 to your computer and use it in GitHub Desktop.
Save eivindingebrigtsen/2488440 to your computer and use it in GitHub Desktop.
var objectify = function () {
var glob = typeof arguments[0] == 'object' ? arguments[0] : window;
var a, b, i, l, n,v, ext = arguments;
for (a in ext) {
i = ext[a];
l = null, n = null;
if (typeof i == 'string') {
if (i.match('.')) {
l = i.split('.');
for (b in l) {
v = null;
v = l[b].split('=');
if(v.length>1){
l[b] = v[0];
}
if (n && l[b] in n) {
n = n[l[b]];
continue;
} else if (b == 0 && l[b] in glob) {
n = glob[l[b]];
continue;
}
if(v[1]){
n[l[b]] = v[1];
continue;
}
n = (n ? n[l[b]] = {} : glob[l[b]] = {});
}
}
}
}
return glob;
};
var OBJ = {};
objectify( OBJ,
'pension.plan.year1.total.payout=100000',
'insurance.private.house',
'bank.accounts.transactions.march.length=34',
'pension.payout.now=10'
);
{
"pension": {
"plan": {
"year1": {
"total": {
"payout": "100000"
}
}
},
"payout": {
"now": "10"
}
},
"insurance": {
"private": {
"house": {}
}
},
"bank": {
"accounts": {
"transactions": {
"march": {
"length": "34"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment