admin管理员组文章数量:1318336
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 e 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 e up with a good solution yet. Another help would be so greatly appreciated!
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 e 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 e up with a good solution yet. Another help would be so greatly appreciated!
Share Improve this question edited Apr 20, 2012 at 20:56 Niko 26.7k9 gold badges96 silver badges112 bronze badges asked Apr 20, 2012 at 20:44 mattacularmattacular 1,88913 silver badges17 bronze badges 3- possible duplicate of Javascript nested objects from string and JavaScript function to create a structured Object from a String? – Matt Ball Commented Apr 20, 2012 at 20:48
- It isn't a duplicate of those, I want to modify a certain property of an existing object and not touch the rest of the object. So the obj I made above already has values set: 'obj.a', 'obj.b', 'obj.c' and i just want to make a change to obj.c.nest – mattacular Commented Apr 20, 2012 at 20:57
- It's almost exactly the same logic, though. – Matt Ball Commented Apr 20, 2012 at 21:07
4 Answers
Reset to default 6My 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/jfriend00/Sxz2z/
This is one way of doing it:
function setDepth(obj, path, value) {
var levels = path.split(".");
var curLevel = obj;
var i = 0;
while (i < levels.length-1) {
curLevel = curLevel[levels[i]];
i++;
}
curLevel[levels[levels.length-1]] = value;
}
Working demo.
I modified Elliot's answer to let it add new nodes if they don't exist.
var settings = {};
function set(key, value) {
/**
* Dot notation loop: http://stackoverflow./a/10253459/607354
*/
var levels = key.split(".");
var curLevel = settings;
var i = 0;
while (i < levels.length-1) {
if(typeof curLevel[levels[i]] === 'undefined') {
curLevel[levels[i]] = {};
}
curLevel = curLevel[levels[i]];
i++;
}
curLevel[levels[levels.length-1]] = value;
return settings;
}
set('this.is.my.setting.key', true);
set('this.is.my.setting.key2', 'hello');
Just do it like this:
new Function('_', 'val', '_.' + path + ' = val')(obj, value);
In your case:
var obj = {
'a': 1,
'b': 2,
'c': {
'nest': true
}
};
new Function('_', 'val', '_.c.nest' + ' = val')(obj, false);
本文标签: javascriptSetting a depth in an object literal by a string of dot notationStack Overflow
版权声明:本文标题:javascript - Setting a depth in an object literal by a string of dot notation? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742042579a2417616.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论