Created
December 31, 2013 09:01
-
-
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
This file contains 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
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