I want to calculate the min, max, and average values for nested data that already have these values precalculated. Essentially, I'm looking for the average of averages, min of min, and max of max.
I have a large dataset that includes the min, max, and average scores for each user:
[
{
"name": "Joe",
"data": [
[
22323,
180.269944190979,
{
"min": 148608,
"max": 486656,
"avg": 123
}
],
[
12312,
180.269944190979,
{
"min": 148608,
"max": 486656,
"avg": 2
}
],
[
232,
180.269944190979,
{
"min": 148608,
"max": 486656,
"avg": 2323
}
]
]
},
{
"name": "Miranda",
"data": [
[
1641466800000,
180.269944190979,
{
"min": 148608,
"max": 486656,
"avg": 123
}
],
[
12312,
180.269944190979,
{
"min": 148608,
"max": 486656,
"avg": 2
}
],
[
232,
180.269944190979,
{
"min": 148608,
"max": 486656,
"avg": 2323
}
]
]
},
{
"name": "Eli",
"data": [
[
1641468540000,
5630.96112537384,
{
"min": 0,
"max": 384048,
"avg": 237031.22959263306
}
],
[
1641468420000,
5688.5411739349365,
{
"min": 157216,
"max": 384048,
"avg": 248715.63971152372
}
],
[
1641468300000,
5832.6060581207275,
{
"min": 0,
"max": 372560,
"avg": 226729.37108433736
}
],
[
1641468360000,
5836.711280345917,
{
"min": 0,
"max": 384048,
"avg": 274216.2701236057
}
]
]
}
]
I'm struggling to calculate the average, min, and max values within each object. For example, I want the following result:
[
{
"name": "Joe",
"details": {
"min": 0,
"max": 12332,
"avg": 9594.43435
}
},
{
"name": "Miranda",
"details": {
"min": 0,
"max": 65443,
"avg": 1231233.2442
}
},
{
"name": "Eli",
"details": {
"min": 0,
"max": 312321,
"avg": 544545.3345
}
}
]
I've attempted to use the reduce
function but I'm unable to retain the previous value.
I struggle with lodash:
data.reduce((acc, val: any) => {
acc.min = acc.min + val[2].min;
return acc;
}, { min: 0, max: 0, avg: 0 })