Skip to content

Instantly share code, notes, and snippets.

@qcom
Created December 31, 2013 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qcom/8194273 to your computer and use it in GitHub Desktop.
Save qcom/8194273 to your computer and use it in GitHub Desktop.
update an object given a slash-delimited path (where each element in path is a property) and a value
function update(path, val, obj) {
(function r(keys, o) {
if (keys.length > 1) {
var current = keys.shift();
if (!o[current])
o[current] = isNaN(parseInt(keys[0])) ? {} : [];
r(keys, o[current]);
} else {
o[keys[0]] = val;
}
})(path.split('/'), obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment