I have an object that contains other objects, such as:
let foo = {
a: {
b: {},
c: {}
},
d: {
e: {}
}
};
Now I want to transform this into an array of objects, where the keys of the first two levels form a key / value pair, such as:
let transformedFoo = [
{ outer: 'a', inner: 'b' },
{ outer: 'a', inner: 'c' },
{ outer: 'd', inner: 'e' }
];
My current approach looks like this:
let fooTransformed = [];
Object.keys(foo).forEach(function (outerKey) {
Object.keys(foo[outerKey]).forEach(function (innerKey) {
fooTransformed.push({
outer: outerKey,
inner: innerKey
});
});
});
It works, but I think it's not "nice" (i.e., it's not nice to have two nested loops). Is there a better way on how to achieve this (I could imagine that there is a quite elegant purely functional solution, but I can't think of any)?