I have a situation where I need to zip two Objects together retaining both of the values. I can iterate through both the objects and build an array of all the keys.
var traverse = function (obj, chain) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
var tempChain = [].concat(chain || []);
tempChain.push(prop);
if (typeof obj[prop] === 'object') {
traverse(obj[prop], tempChain);
}
console.log(tempChain);
}
}
};
Passing in the following:
traverse({
'a': {
'b': 'hello world',
'b1': 'hello world 1',
'c': {
'd': 'dello'
}
}
})
Will return me:
[a]
[a,b]
[a,b1]
[a,c]
[a, c, d]
So I now have an array of nested properties in an object. How can I access essentially obj[[a,c,d]]? I know I can solve the problem through eval but I can't trust the content.
eval('window.' + ['a','c','d'].join('.'));
If I can loop through that array and check to see if the property exists in both of them, then build a new object of the combined 'zipped' values.