Skip to content

Instantly share code, notes, and snippets.

@edtoken
Last active August 29, 2015 14:17
Show Gist options
  • Save edtoken/327fae62ac483f2cd04e to your computer and use it in GitHub Desktop.
Save edtoken/327fae62ac483f2cd04e to your computer and use it in GitHub Desktop.
var makeNestedObject = function(key, value, pattern){
var items = key.split(pattern).reverse();
var obj = value;
for(var i=0;i<items.length;i++){
var newObj = {};
newObj[items[i]] = obj;
obj = newObj;
}
return obj;
};
var obj = makeNestedObject('key1_key2_key3_key4', 'value', '_');
/**
* out: {"key1":{"key2":{"key3":{"key4":"value"}}}}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment