I have a Json file with an unknown depth of nested children, and i need to read that with javascript, and delete an element somewhere within the object. I have the path of the child to delete stored in an array called nestPath (example [0,0,3] where the path would be [0]->[0]->[3]). The problem is that i do not know how to dynamically target the object directly without using a variable to reference the Json object, but if i try to delete the variable it will only delete the reference, not the actual object within the Json.
I would like to dynamically access it with the path given by nestPath so that with an input of [0,1,2,3], i would be able to access: jsonObject[0][1][2][3] without using a variable, so that i can delete it.
The code i tried:
var nestPath = [0,0,1];
var stateObject = menuToolState['menu'];
for (var i = 0; i < nestPath.length; i++) {
if (i > 0) {
stateObject = stateObject['links'][nestPath[i]];
} else {
stateObject = stateObject[nestPath[i]];
}
}
delete stateObject;
menuToolState:
{
"menu": [
{
"name": "0",
"links": [
{
"name": "0-0",
"links": [
{
"name": "0-0-0",
"links": []
},
{
"name": "0-0-1 (delete this)",
"links": []
}
]
}
]
}
]
}
With this code i am able to access the information within the object, but i am not able to delete it as delete just deletes the reference. How can i achieve this?