I have complected metadata dictionary that has to be modified on web-page.
Example segment of a structure:
MD = {
"request_id": 1,
"environment": "Dev",
"groups": [{
"group_number": 1,
"files": [{
"file_id": 1,
"stages": {
1: {
"jobid": 1,
"status": true
},
2: {
"jobid": 2,
"status": true
},
4: {
"calls": [{
"jobid": 3,
"value": 0
},
{
"jobid": 4,
"value": 1
}
]
},
3: {
"jobid": 5,
"status": true
},
5: {
"streams": [{
"jobid": 6,
"Value": 0
},
{
"jobid": 7,
"value": 1
}
]
}
}
}]
}]
}
I want to be able to store the location of a field in run-time and to access it by a key.
I plan to store location like this:
for the field
MD["groups"][0]["files"][0]["stages"][1]
the key would be this array
["groups",0,"files",0,"stages",1]
.
But the question now is how to access this key in automated way? Due to the fact that I don't know maximum depth of MD, all I came up with is recursion:
function get_element(dict, key_arr){
if (key_arr.length == 1){
return dict[key_arr[0]]
} else {
return get_element(dict[key_arr[0]], key_arr.splice(1,key_arr.length))
}
}
get_element(MD, ["groups",0,"files",0,"stages",1])
Is there any better way to store location in multilevel dictionary or to access it?