I am looking to transform the current data structure:
[ { sectionName: 'SectionOne',
ingredients: [ {ingredient: 'sugar'}, {ingredient: 'flour'} ]},
{ sectionName: 'SectionTwo',
ingredients: [ {ingredient: 'eggs'}, {ingredient: 'water'} ] },
]
into this new format:
{ SectionOne:
{ sectionName: 'SectionOne',
ingredients: {
sugar: { ingredient: 'sugar' },
flour: { ingredient: 'flour' }
}
},
{ SectionTwo:
{ sectionName: 'SectionTwo',
ingredients: {
eggs: { ingredient: 'eggs' },
water: { ingredient: 'water' }
}
},
}
In essence, I aim to utilize the object keys from each array to create an object.
An example of the data structure can be found on this jsfddle along with my attempt at achieving this transformation.
Despite trying methods like lodash or vanillaJS, I have only succeeded in converting the outer array. Recursively applying _.mapKeys(), for loops, or similar techniques to reach the desired structure has proven challenging. I believe there may be a simple solution that is eluding me at the moment.
Your assistance on this matter would be greatly appreciated!