Last active
September 23, 2015 05:05
-
-
Save amcmillan01/3888aac1594075c3c6e5 to your computer and use it in GitHub Desktop.
converts a dot notation string into an object
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
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": {} | |
} | |
} | |
} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice!