Is it possible to dynamically set keys when using this syntax as shown in this example?
In this example, the changeValue function changes the state object show at the top. The changeValue function takes an array of string keys to be used as a way to dynamically set the value.
This code does not work and I am not looking for alternative approaches: (within reasonable alternation)
const state = {
profile: {
isFollowing: false,
},
person: {
characteristics: {
gender: "male"
}
}
}
console.log(state.profile.isFollowing) //false
console.log(state.person.characteristics.gender) //male
function changeValue(keys, value){
state[keys] = value
}
console.log(state.profile.isFollowing) //true
console.log(state.person.characteristics.gender) //female
changeValue(["profile", "isFollowing"], true) //<-- using an array of strings
//AFTER STATE CHANGE
//const state = {
// profile: {
// isFollowing: true,
// },
// person: {
// characteristics: {
// gender: "male"
// }
// }
//}
changeValue(["person", "characteristics", "gender"], "female") //<-- using an array of strings
//AFTER STATE CHANGE
//const state = {
// profile: {
// isFollowing: true,
// },
// person: {
// characteristics: {
// gender: "female"
// }
// }
//}
I don't want to have to manually define the [keys] within the changeValue function.
Is this possible?