My brain is melting from this... I'm trying to accomplish the following:
I have an array of objects which have also have arrays:
const data = [
{
seatChartResults: {
'10th': [40, 40, 40, 39, 39, 38, 38, 38, 38, 38],
'90th': [44, 44, 44, 45, 45, 46, 46, 46, 47, 47],
avg: [42, 42, 42, 42, 42, 42, 42, 42, 42, 42],
}
},
{
seatChartResults: {
'10th': [41, 40, 40, 39, 39, 39, 38, 38, 38, 38],
'90th': [43, 44, 45, 45, 45, 46, 46, 46, 47, 47],
avg: [42, 42, 42, 42, 42, 42, 42, 42, 42, 42],
}
},
]
Now I want to achieve something that will get the average of each index of these keys, so for example:
(data[0].seatChartResults['10th'][0] + data[1].seatChartResults['10th'][0]) / 2
and so on ...
The end result is an aggregation of the objects into the same structure:
{ // With all averages aggregated
seatChartResults: {
'10th': [41, 40, 40, 39, 39, 39, 38, 38, 38, 38],
'90th': [43, 44, 45, 45, 45, 46, 46, 46, 47, 47],
avg: [42, 42, 42, 42, 42, 42, 42, 42, 42, 42],
}
},
This is what I have now:
const avgSeatChartResults = (allLambdas) => {
return allLambdas.reduce((acc, { seatChartResults }) => {
Object.keys(seatChartResults).forEach(key => {
const allKeys = [acc.seatChartResults[key] = seatChartResults[key]]
allKeys.map(item => item.reduce((acc, currValue) => acc + currValue, 0) / item.length )
})
return acc
}, { seatChartResults: {} })
}
but... I'm not sure if correctly doing this. Please help.