How to extract the values ​​of a hierarchical object when the references are in an array?

advertisements

I have the following object

{
"locations": {
    "Base 1": {
        "title": "This is base 1",
        "Suburb 1": {
            "title": "Suburb 1 in Base 1",
            "Area A": {
                "title": "Title for Area A",
                "Street S1": {
                    "title": "Street S1 title"
                },
                "Street C4": {
                    "title": "Street C4 title"
                },
                "Street B7": {
                    "title": "Street B7 title"
                }
            },
            "Another Area": {
                "title": "Title for Area A",
                "Street S1": {
                    "title": "Street S1 title"
                },
                "Street C4": {
                    "title": "Street C4 title"
                },
                "Street B7": {
                    "title": "Street B7 title"
                }
            }
        },
        "Another Suburb": {
            "title": "Suburb 1 in Base 1",
            "Area A": {
                "title": "Title for Area A",
                "Street S1": {
                    "title": "Street S1 title"
                },
                "Street C4": {
                    "title": "Street C4 title"
                },
                "Street B7": {
                    "title": "Street B7 title"
                }
            },
            "Another Area": {
                "title": "Title for Area A",
                "Street S1": {
                    "title": "Street S1 title"
                },
                "Street C4": {
                    "title": "Street C4 title"
                },
                "Street B7": {
                    "title": "Street B7 title"
                }
            }
        }
    },
    "Base2": {}
}
}

I'm given arrays to fetch "titles" from the "locations" object and each array can be different. I know I can access individual values like so :

locations["Base 1"]["title"]
locations["Base 1"]["Another Suburb"]["title"]
locations["Base 1"]["Another Suburb"]["Area A"]["title"]
etc etc.

But I'm not sure how to get the value of title if I'm given arrays like so :

AnArray = ["Base 1", "title"];
AnArray = ["Base 1", "Another Suburb", "title"];
AnArray = ["Base 1", "Another Suburb", "Area A", "title"];
AnArray = ["Base 1", "Another Suburb", "Another Area", "title"];

Is there a way to parse / work with these arrays so each returns the correct title value from the locations object?

I have to fetch the value of the title in each case, and I'm not even sure where to start. I tried joining the array and then fetching the 'title' values but that didn't seem to work.

Noob here, so please don't mind if the question sounds stupid / or makes no sense.

So the question is, how do I fetch values from a hierarchical object when the references are in an array ?


function getNested(obj, ar_keys) {
    var innerObj = obj;
    for(var i=0,il=ar_keys.length; i<il; i++){
      innerObj = innerObj[ar_keys[i]];
    }
    return innerObj;
}

You would call it with

 getNested(x['locations'], ar_keys);

where x is your object and ar_keys is the array of keys.