Assuming a parent container object with the form:
var parentObj =
{
obj: {
key: value,
key2: value,
}
obj2: {
key3: value
}
...
}
Also if we want to include each sub-key only once,
we have
var objNameArray = ['obj','obj2','objN'];
var allValues = [];
var usedKeys = {};
for(var i = 0; i < objNameArray.length; ++i){
// If the parent object does not have a matching sub-object, skip to the
// next iteration of the loop
if(!parentObj[objNameArray[i]]) {
continue;
}
var currentObj = parentObj[objNameArray[i]];
var subKeys = Object.keys(currentObj);
for(var j = 0; j < subKeys.length; ++j) {
if(usedKeys[subKeys[j]]) {
continue;
}
usedKeys[subKeys[j]] = true;
allValues.push(currentObj[subKeys[j]]);
}
}
All the values from all the keys in each sub-object will then be in the array allValues, and all the keys from each sub-object will be available by calling Object.keys(usedKeys);. Note that if sub-objects can also have sub-objects, this strategy needs to be adapted to support recursion.
?>{and the property names areobj:,obj2...