I have a JavaScript object that looks something like this:
obj = {
person: {
male: true,
age: 10
},
state: {
quit: false,
rain: -3
},
game: {
settings: {
lang: 'en',
os: 'win',
ver: 10,
},
},
next: 55,
last: 10,
};
I'd like to create a function that can be used to set any of the obj's values like this:
function updateObj( property /*: string[]*/, value /*: any*/ ) {
obj[property[0]][property[1]] = value; <-- How to generalize?
}
So then I can do things like:
updateObj( ['person', 'male'], false );
updateObj( ['state', 'rain'], 19 );
However the current implementation won't work properly if the property parameter length is not equal to 2. How can this line of code:
obj[property[0]][property[1]] = value;
be generalized to work with any array size of property?
obj.state.quit = false.What is the function meant to do that accessing the object directly can't?obj['person']['male'] = false?updateObj(['game','settings','ver'], 11 )and alsoupdateObj(['next'], 100 )to also work