1

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 })
3
  • 1
    Please put your attempt and a small sample of this data in a functioning snippet demo using the editor. Commented Feb 3, 2022 at 16:47
  • 1
    why are the values same for each element in the expected result Commented Feb 3, 2022 at 16:48
  • @cmgchess i just copy pasted the values, its just what I want to do, doesn't have to be that the date is the same Commented Feb 3, 2022 at 16:50

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Although a great solution, I wonder how much of this can be generalized. For instance, if you had to add something other than min, max and average, such as standard deviation, you'd have to modify this code in multiple places. Could there be a set operation (a distant cousin of transducers) that will have the advantage of traversing over the list once, but performing generic operations that are independent of one another.
For instance, the logic of calculating the value of min and max are independent of one another, but they are right there, being done, close to each other, in the same piece of code, simply to do as much as possible in the single iteration. Transducers address this but they are about chaining dependent operations within the iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.