Skip to content

Instantly share code, notes, and snippets.

@L3V147H4N
Last active March 10, 2016 14:51
Show Gist options
  • Save L3V147H4N/efbc65a5ac9e834889b2 to your computer and use it in GitHub Desktop.
Save L3V147H4N/efbc65a5ac9e834889b2 to your computer and use it in GitHub Desktop.
Javscript Set Deep Value
function setDeepValue (path, value, separator = '.', target) {
path = path.split(separator);
var obj = null;
var res = Object.assign({}, (target ? target : this));
var current = res;
for (var i = 0; i < path.length; i++) {
if (i === (path.length - 1)) {
obj = value;
} else {
obj = {};
}
current[path[i]] = obj;
current = obj;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment