javascript 动态更新/创建对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8178357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update/Create object properties dynamically
提问by Eric Martin
I'm creating a feature that provides a get() and set() function to read and update values from a JavaScript object. The trick is that I'm using a dot notated string to specify the properties.
我正在创建一个功能,该功能提供 get() 和 set() 函数来读取和更新 JavaScript 对象的值。诀窍是我使用点符号字符串来指定属性。
I've got the get() working fine, it's the set() that I'm having troubles with.
我的 get() 工作正常,我遇到了麻烦的 set()。
Sample data:
样本数据:
var data = {
person: {
name: 'Fred',
birthday: '19840101',
address: {
street: '123 Main St.',
city: 'Anytown',
state: 'NY',
zip: '123123'
},
family: {
spouse: 'Sally',
children: ['Sam', 'Frank', 'Susan']
}
}
};
get() function:
获取()函数:
function get (path) {
var parts = path.split('.'),
tmp = data;
for (var i = 0, l = parts.length; i < l; ++i) {
if (tmp[parts[i]]) {
tmp = tmp[parts[i]];
}
else {
tmp = null
break;
}
}
return tmp;
}
console.log(get('person.name')); // Fred
console.log(get('person.family.children')); // ['Sam', 'Frank', 'Susan']
console.log(get('person.jobs.apple')); // null
set() function:
设置()函数:
var foo = null;
function set (path, val) {
var parts = path.split('.')
tmp = {},
foo = data;
for (var i = 0, l = parts.length; i < l; ++i) {
if (tmp[parts[i]]) {
tmp = data[parts[i]];
}
else {
tmp = {};
}
if (i+1 === l) {
tmp = val;
}
data[parts[i]] = tmp;
}
}
set('person.name', 'Dave'); // replace existing name
set('person.family.children', ['Tim', 'Matt', 'Kathy']); // replace existing array
set('person.jobs.apple', 'developer'); // should create the necessary properties
console.log(data);
I end up with the following object:
我最终得到以下对象:
obj = {
person: {},
name: "Dave",
family: {},
children: ["Tim","Matt","Kathy"],
jobs: {},
apple: "developer"
}
Any thoughts on how to accomplish this?
关于如何实现这一点的任何想法?
采纳答案by Daveo
You do not really need any functions at all for this.
为此,您根本不需要任何功能。
See this
看到这个
var data = {
person: {
name: 'Fred',
birthday: '19840101',
address: {
street: '123 Main St.',
city: 'Anytown',
state: 'NY',
zip: '123123'
},
family: {
spouse: 'Sally',
children: ['Sam', 'Frank', 'Susan']
}
}
};
alert(data.person.name);
data['person']['name'] = 'Tim';
alert(data['person']['name']);
There is also this answer which may help
还有这个答案可能会有所帮助
Javascript object key value coding. Dynamically setting a nested value
回答by Fyodor Soikin
It appears that you have some typos in your set
code.
您的set
代码中似乎有一些拼写错误。
The line tmp = data[parts[i]];
should really be tmp = tmp[parts[i]];
.
And then you probably want to put that into a new variable - say, newTmp
, - so that you can assign it back into the object later:
这条线tmp = data[parts[i]];
真的应该是tmp = tmp[parts[i]];
。然后你可能想把它放到一个新变量中 - 比如newTmp
,, - 这样你就可以稍后将它分配回对象:
tmp[parts[i]] = newTmp;
tmp = newTmp;
回答by Sachin Nayak
Your problem seems to be the data[parts[i]] = tmp; at the end of the set function.
你的问题似乎是 data[parts[i]] = tmp; 在 set 函数的末尾。