My question is slightly similar to this one.
So, say I have an object like this:
var obj = {
'a': {
'b': {
'c': 1,
'd': 2
},
'e': 3
},
'f': 4,
'g': 5
};
I want to run that through a function and create an array that looks something like this:
var arr =
'a',
'a.b',
'a.b.c',
'a.b.d',
'a.e',
'f',
'g'
];
The purpose of that array is so that I can later loop through objects with the same hierarchial format in the same exact same way. I'm unsure about how to do this step.
So, given obj
and arr
, it would use a for
loop to access all the key-value pairs in obj
. As far as I know, you can't access a nested property like this: obj['a.b.c']
, so I'm not exactly sure how to do this.
Clarification Edit:
After this array is created, I'm wondering how to use it to loop through objects of the same format in the way described by the array. For example
function iterateAnotherObjectWithSameFormat(aObj) {
for (var i = 0; i < arr.length; i++) {
// access aObj['a'], then aObj['a.b'], then aObj['a.b.c'], etc..
}
}