0

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?

Alex
  • 731
  • 1
  • 6
  • 21

2 Answers2

1

You could reduce the keys by returning the nested object for each key.

function get_element(dict, keys) {
    return keys.reduce((o, k) => o[k], dict);
}

var 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 }] } } }] }] };

console.log(get_element(md, ["groups", 0, "files", 0, "stages", 1]));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Maybe like this?

function getElement(dict, keys) {
  let element = dict;

  keys.forEach((key) => {
    element = element[key];
  });

  return element;
}

Or with reduce

function getElement(dict, keys) {  
  return keys.reduce((element, key) => element[key], dict);
}
erksch
  • 501
  • 4
  • 14