I would like to generate the min/max/average of "nested" values that already have min/max/average precalculated. So basically average of average, min of min, max of max, hope I'm clear enough
I have a huge list of data that contains the min/max/average of each user score:
[
{
"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 not sure how could I generate the average/min/max of all of these inside each object, for example this:
[
{
"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 tried with reduce
but I can't get it to "remember" the previous value
I'm not very good at lodash:
data.reduce((acc, val: any) => {
acc.min = acc.min + val[2].min;
return acc;
}, { min: 0, max: 0, avg: 0 })
You need a combination of map
and reduce
to produce the output you're after:
const input=[{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:[[16414668e5,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:[[164146854e4,5630.96112537384,{min:0,max:384048,avg:237031.22959263306}],[164146842e4,5688.5411739349365,{min:157216,max:384048,avg:248715.63971152372}],[16414683e5,5832.6060581207275,{min:0,max:372560,avg:226729.37108433736}],[164146836e4,5836.711280345917,{min:0,max:384048,avg:274216.2701236057}]]}]; const result = input.map(item => { const details = item.data.reduce( (acc,i) => { const val = i[2]; acc.min = Math.min(val.min, acc.min); acc.max = Math.max(val.max, acc.max); acc.sum += val.avg return acc; },{min:Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY, sum:0}) return { name: item.name, details:{ min: details.min, max: details.max, avg: details.sum/item.data.length } } }); console.log(result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.