I have an array variable containing JSON data and I need to update specific values within the array using string keys. Here is a snippet of what my array looks like:
{
"all": [
{
"image":{
"URL":"img/img1.jpeg",
"font": "sfsdfsdf",
"color": "sfsdfs"
},
"music": {
"URL":"fsfsfd",
"time": {
"start":"sfsdf",
"end":"qdqsd"
}
}
},
{
"image":{
"URL":"img/img2.jpeg",
"font": "sfsdfsdf",
"color": "sfsdfs"
},
"music": {
"URL":"fsfsfd",
"time": {
"start":"sfsdf",
"end":"qdqsd"
}
}
}
]
}
I also have a second array that contains the path split into segments for the key I want to update, as shown below:
var path = ["all", 0, "image", "font"]
Currently, I am iterating through the path
variable and checking if the key exists in my JSON data. However, I am unsure how to update the JSON array without changing its structure...
For instance, I would like to change the value of myArray[all][0][image][font]
to "My Other Value"
The ultimate objective is to update my JSON array and save the changes to a JSON file.
EDIT :
I have discovered a solution from this resource : Dynamically updating a JavaScript object from a string path