I have an object like this
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
}
}
};
I want to write a function to change the data dynamically like this Funtkion
function updateUserData(userName, key, value) {
userobj[userName][key] = value;
}
This works if only to change the first level key
updateUserData('user1', 'firstName', 'David');
can someone tell me how I can change the function so that I can change key into other levels too? Perhaps with an array as parameter? Like this
updateUserData('user1', ['address', 'company', 'location'], 'David');