I have the following array, which has an object holding nested arrays that in turn have objects of their own:
var list = [{
one: [{key: 1, value: 'eng1'}, {key: 2, value: 'eng2'}],
two: [{key: 1, value: 'esp1'}, {key: 2, value: 'esp2'}]
}];
I want to group the data above by the key property inside the objects of the nested arrays. The following is the desired structure:
var grouped = [
{
key: 1,
group: {
one: {
value: 'eng1'
},
two: {
value: 'esp1'
}
}
},
{
key: 2,
group: {
one: {
value: 'eng2'
},
two: {
value: 'esp2'
}
}
}
]
I have tried so many ways to achieve the above structure but to no avail. I have tried using reduce but that was mainly applicable if the objects were not deeply nested. My problem is the fact that the arrays are nested inside the objects and the key is embedded in those objects. Your help would be greatly appreciated.
The requirement is to not use any library such as underscore or lodash. I have to get this working with plain JS.