I have an array as follow:
const myArray = [
{
name: 'banana',
quotas: [
{
title: 'innerBanana1',
percent: 0.3
},
{
title: 'innerBanana2',
percent: 0.4
}
]
},
{
name: 'apple',
quotas: [
{
title: 'innerApple1',
percent: 0.6
},
{
title: 'innerApple2',
percent: 0.2
}
]
}
]
I would like to sum all the percent in the quotas array only if they belong to the same external object (aka: the name is the same).
Expected Result
finalArray = [
{ name: 'banana', percent: 0.7 },
{ name: 'apple', percent: 0.8 }
]
I tried
const sum = quotaGroups
.map(quotaGroup => quotaGroup.quotas)
.reduce((accumulator, groupedQuota) => {
return accumulator + groupedQuota[0].percent
})
But it clearly does not work. I am missing the link on how to sum only the quotas of the inner object if the name is the same