So I want to change the content of object "alfa" according to a list of keyNames according to the newValue.
let alfa = {
bravo : {
charlie : {
delta : "echo"
}
}
}
const newValue = { foxtrot : "golf" }
I want to do something like this, dynamically:
alfa[keyNames] = newValue
// where if
const keyNames = ["bravo", "charlie"]
// would do:
alfa["bravo"]["charlie"] = newValue
// or if
const keyNames = ["bravo"]
// would do:
alfa["bravo"] = newValue
How to do that?
I know I can do this, but there is probably a way to cover every possibles cases...
if(keyNames.length === 1){ alfa[keyNames[0]] = newValue }
if(keyNames.length === 2){ alfa[keyNames[0]][keyNames[1]] = newValue }
...