Setting a depth in an object literal by a string of dot notation?
javascript
Solution
My version:
function setDepth(obj, path, value) {
var tags = path.split("."), len = tags.length - 1;
for (var i = 0; i < len; i++) {
obj = obj[tags[i]];
}
obj[tags[len]] = value;
}
Working demo: http://jsfiddle.net/jfriend00/Sxz2z/
Problem
There are plenty of solutions out there to check/access an object literal giving a string of dot notation, but what I need to do is SET an object literal based on a string of dot notation. It is very technical why I need to do this, and if it isn't feasible I will come up with a different solution. Here is what I'd like to do: ``` var obj = { 'a': 1, 'b': 2, 'c': { 'nest': true } }; ``` I'd like a function that would work something like this: ``` setDepth(obj, 'c.nest', false); ``` That would change the obj to this: ``` var obj = { 'a': 1, 'b': 2, 'c': { 'nest': false } }; ``` I've been trying to an hour and haven't been able to come up with a good solution yet. Another help would be so greatly appreciated!