Imagine having this specific object structure:
var obj = {
level1 :{
level2: {
level3: {
title: "champion"
}
}
}
}
Now the goal is to update the title
key using a provided string (note that it's a string, not an actual variable)
like so:
let myString = "level1.level2.level3.title"; // please note that the value of myString is fetched dynamically
One possible method could be:
obj[myString] = "ultimate-champion";
Sadly, the above approach doesn't produce the desired outcome.
Furthermore - there are instances where an undefined object needs to be addressed by defining it with a new empty object.
For instance, If starting off with the following object:
var obj = {
level1 : {}
}
}
The objective remains the same: altering the obj
in order to reach level3.winner as previously shown.
Quick reminder:
obj[myString] = "ultimate-champion";
How can this be accomplished?