Last active
December 20, 2015 12:59
-
-
Save isochronous/6134976 to your computer and use it in GitHub Desktop.
convert a flat object with namespaced keys to a heirarchical object
This file contains hidden or 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
/** | |
* Converts a "flat" object with namespaced keys into a hierarchical object | |
* @param {object} flat - A "flat" object with namespaced keys to turn into a hierarchical object | |
* @returns object - the hierarchical object | |
*/ | |
var _paths2obj = function (flat) { | |
var root = {}, | |
keys = _.keys(flat), | |
parts, entry, currentPart, pointer, i, il; | |
for (entry in keys) { | |
parts = entry.split('.'); | |
pointer = root; | |
for (i=0, il=parts.length; i < il; i++) { | |
currentPart = parts[i]; | |
pointer[currentPart] = pointer[currentPart] || {}; | |
pointer = pointer[currentPart]; | |
} | |
pointer = flat[entry]; | |
} | |
return root; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment