我有一个像这样的对象
var userobj = {
user1: {
firstName: 'abc',
lastName: 'abc',
age: 29,
address: {
company: {
location: 'xyz',
city: 'xyz',
street: 'xyz',
city: 'xyz'
},
location: 'xyz',
city: 'xyz',
street: 'xyz',
city: 'xyz'
},
payment: {
visa: false,
master: false,
paypal: true
},
},
user2: {
firstName: 'abc',
lastName: 'abc',
age: 29,
address: {
company: {
location: 'xyz',
city: 'xyz',
street: 'xyz',
city: 'xyz'
},
location: 'xyz',
city: 'xyz',
street: 'xyz',
city: 'xyz'
},
payment: {
visa: false,
master: false,
paypal: true
}
}
};
我想编写一个函数来动态更改数据,就像这个 Funtkion
function updateUserData(userName, key, value) {
userobj[userName][key] = value;
}
这仅适用于更改第一级密钥
updateUserData('user1', 'firstName', 'David');
有人可以告诉我如何更改功能,以便我也可以将密钥更改为其他级别吗?也许用数组作为参数?像这样
updateUserData('user1', ['address', 'company', 'location'], 'David');