Skip to content

Instantly share code, notes, and snippets.

@wkronemeijer
Last active June 18, 2016 17:53
Show Gist options
  • Save wkronemeijer/6960075 to your computer and use it in GitHub Desktop.
Save wkronemeijer/6960075 to your computer and use it in GitHub Desktop.
set key value paths for javascript
function setKeyValue(object, path, value) {
var prop_cont_pair = path.split('.');
var prop = prop_cont_pair[0];
var cont = prop_cont_pair.slice(1).join('.');
if (cont === '') {
object[prop] = value;
} else {
if(typeof object[prop] === 'undefined') {
object[prop] = {};
};
setKeyValue(object[prop], cont, value);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment