Skip to content

Instantly share code, notes, and snippets.

@amcmillan01
Last active September 23, 2015 05:05
Show Gist options
  • Save amcmillan01/3888aac1594075c3c6e5 to your computer and use it in GitHub Desktop.
Save amcmillan01/3888aac1594075c3c6e5 to your computer and use it in GitHub Desktop.
converts a dot notation string into an object
function dotNotationToObject (str) {
var obj = {};
var prev = null;
str.split('.').forEach(function(prop){
//console.log('prop', prop);
if(prev) {
//console.log('prev', prev);
//obj = prev;
prev[prop] = {};
prev = prev[prop];
} else {
obj[prop] = {};
prev = obj[prop];
}
});
return obj;
}
var obj = dotNotationToObject('top.foo.bar.blah.ids');
console.log(JSON.stringify(obj, null, 2));
/*
// output
{
"top": {
"foo": {
"bar": {
"blah": {
"ids": {}
}
}
}
}
}
*/
@reqshark
Copy link

nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment