Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sum value from multiple array of object in javascript

Can anyone help me on how to sum and then divide by the length of the array to get the average between dynamic multiple array of objects in javascript, the structure of my data as below. (example two array of objects, but might be more then 2 array of object)

"result": [
    {
        "id": "ioimall",
        "color": "hsla(147, 47%, 55%, 1)",
        "data": [
            {
                "x": "2023-04-01",
                "y": 13.3202
            },
            {
                "x": "2023-04-02",
                "y": 11.8434
            },
            {
                "x": "2023-04-03",
                "y": 17.2365
            },
            {
                "x": "2023-04-04",
                "y": 13.2256
            },
            {
                "x": "2023-04-05",
                "y": 23.9547
            },
            {
                "x": "2023-04-06",
                "y": 17.1793
            },
            {
                "x": "2023-04-07",
                "y": 15.776
            },
            {
                "x": "2023-04-08",
                "y": 11.3356
            },
            {
                "x": "2023-04-09",
                "y": 6.6018
            },
            {
                "x": "2023-04-10",
                "y": 4.3196
            },
            {
                "x": "2023-04-11",
                "y": 4.8547
            },
            {
                "x": "2023-04-12",
                "y": 4.1935
            },
            {
                "x": "2023-04-13",
                "y": 4.6628
            },
            {
                "x": "2023-04-14",
                "y": 5.9717
            },
            {
                "x": "2023-04-15",
                "y": 3.8766
            },
            {
                "x": "2023-04-16",
                "y": 3.6379
            },
            {
                "x": "2023-04-17",
                "y": 4.7511
            },
            {
                "x": "2023-04-18",
                "y": 5.2891
            },
            {
                "x": "2023-04-19",
                "y": 4.7904
            },
            {
                "x": "2023-04-20",
                "y": 7.5441
            },
            {
                "x": "2023-04-21",
                "y": 191.0336
            },
            {
                "x": "2023-04-22",
                "y": 0
            },
            {
                "x": "2023-04-23",
                "y": 0
            },
            {
                "x": "2023-04-24",
                "y": 0
            },
            {
                "x": "2023-04-25",
                "y": 0
            },
            {
                "x": "2023-04-26",
                "y": 0
            },
            {
                "x": "2023-04-27",
                "y": 0
            },
            {
                "x": "2023-04-28",
                "y": 0
            },
            {
                "x": "2023-04-29",
                "y": 0
            },
            {
                "x": "2023-04-30",
                "y": 0
            }
        ]
    },
    {
        "id": "mytown",
        "color": "hsla(221, 79%, 74%, 1)",
        "data": [
            {
                "x": "2023-04-01",
                "y": 4.8123
            },
            {
                "x": "2023-04-02",
                "y": 5.5642
            },
            {
                "x": "2023-04-03",
                "y": 5.2656
            },
            {
                "x": "2023-04-04",
                "y": 8.7167
            },
            {
                "x": "2023-04-05",
                "y": 5.3078
            },
            {
                "x": "2023-04-06",
                "y": 4.7283
            },
            {
                "x": "2023-04-07",
                "y": 4.1322
            },
            {
                "x": "2023-04-08",
                "y": 5.1783
            },
            {
                "x": "2023-04-09",
                "y": 5.2504
            },
            {
                "x": "2023-04-10",
                "y": 6.7794
            },
            {
                "x": "2023-04-11",
                "y": 5.0806
            },
            {
                "x": "2023-04-12",
                "y": 4.9803
            },
            {
                "x": "2023-04-13",
                "y": 4.3914
            },
            {
                "x": "2023-04-14",
                "y": 5.4071
            },
            {
                "x": "2023-04-15",
                "y": 4.939
            },
            {
                "x": "2023-04-16",
                "y": 4.4732
            },
            {
                "x": "2023-04-17",
                "y": 5.0562
            },
            {
                "x": "2023-04-18",
                "y": 5.0911
            },
            {
                "x": "2023-04-19",
                "y": 4.8266
            },
            {
                "x": "2023-04-20",
                "y": 23.4864
            },
            {
                "x": "2023-04-21",
                "y": 1185.7143
            },
            {
                "x": "2023-04-22",
                "y": 0
            },
            {
                "x": "2023-04-23",
                "y": 0
            },
            {
                "x": "2023-04-24",
                "y": 0
            },
            {
                "x": "2023-04-25",
                "y": 0
            },
            {
                "x": "2023-04-26",
                "y": 0
            },
            {
                "x": "2023-04-27",
                "y": 0
            },
            {
                "x": "2023-04-28",
                "y": 0
            },
            {
                "x": "2023-04-29",
                "y": 0
            },
            {
                "x": "2023-04-30",
                "y": 0
            }
        ]
    }
],

what I would like to sum/reduce is the key y: (value1 + value2)/length (to get the average) inside the key data

Expected result as below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"result": [
{
    "id": "average",
    "color": "hsla(147, 47%, 55%, 1)",
    "data": [
        {
            "x": "2023-04-01",
            "y": 9.06
        },
        {
            "x": "2023-04-02",
            "y": 8.70
        },
        {
            "x": "2023-04-03",
            "y": 11.25
        },
        {
            "x": "2023-04-04",
            "y": 10.97
        },
        {
            "x": "2023-04-05",
            "y": 14.63
        },
        {
            "x": "2023-04-06",
            "y": 10.95
        },
        {
            "x": "2023-04-07",
            "y": 9.95
        },
        {
            "x": "2023-04-08",
            "y": 8.25
        },
        {
            "x": "2023-04-09",
            "y": 5.92
        },
        {
            "x": "2023-04-10",
            "y": 5.54
        },
        {
            "x": "2023-04-11",
            "y": 4.96
        },
        {
            "x": "2023-04-12",
            "y": 4.58
        },
        {
            "x": "2023-04-13",
            "y": 4.52
        },
        {
            "x": "2023-04-14",
            "y": 5.68
        },
        {
            "x": "2023-04-15",
            "y": 4.40
        },
        {
            "x": "2023-04-16",
            "y": 4.05
        },
        {
            "x": "2023-04-17",
            "y": 4.90
        },
        {
            "x": "2023-04-18",
            "y": 5.19
        },
        {
            "x": "2023-04-19",
            "y": 4.80
        },
        {
            "x": "2023-04-20",
            "y": 15.51
        },
        {
            "x": "2023-04-21",
            "y": 688.37
        },
        {
            "x": "2023-04-22",
            "y": 0
        },
        {
            "x": "2023-04-23",
            "y": 0
        },
        {
            "x": "2023-04-24",
            "y": 0
        },
        {
            "x": "2023-04-25",
            "y": 0
        },
        {
            "x": "2023-04-26",
            "y": 0
        },
        {
            "x": "2023-04-27",
            "y": 0
        },
        {
            "x": "2023-04-28",
            "y": 0
        },
        {
            "x": "2023-04-29",
            "y": 0
        },
        {
            "x": "2023-04-30",
            "y": 0
        }
    ]
}

],

>Solution :

If you always have all x values represented in each of the data arrays:

Start with the first data array, and look at each x value. Then, locate all matching objects with that x value. Use reduce to add them together to compute the average.

const arr = [{"id":"ioimall","color":"hsla(147, 47%, 55%, 1)","data":[{"x":"2023-04-01","y":13.3202},{"x":"2023-04-02","y":11.8434},{"x":"2023-04-03","y":17.2365},{"x":"2023-04-04","y":13.2256},{"x":"2023-04-05","y":23.9547},{"x":"2023-04-06","y":17.1793},{"x":"2023-04-07","y":15.776},{"x":"2023-04-08","y":11.3356},{"x":"2023-04-09","y":6.6018},{"x":"2023-04-10","y":4.3196},{"x":"2023-04-11","y":4.8547},{"x":"2023-04-12","y":4.1935},{"x":"2023-04-13","y":4.6628},{"x":"2023-04-14","y":5.9717},{"x":"2023-04-15","y":3.8766},{"x":"2023-04-16","y":3.6379},{"x":"2023-04-17","y":4.7511},{"x":"2023-04-18","y":5.2891},{"x":"2023-04-19","y":4.7904},{"x":"2023-04-20","y":7.5441},{"x":"2023-04-21","y":191.0336},{"x":"2023-04-22","y":0},{"x":"2023-04-23","y":0},{"x":"2023-04-24","y":0},{"x":"2023-04-25","y":0},{"x":"2023-04-26","y":0},{"x":"2023-04-27","y":0},{"x":"2023-04-28","y":0},{"x":"2023-04-29","y":0},{"x":"2023-04-30","y":0}]},{"id":"mytown","color":"hsla(221, 79%, 74%, 1)","data":[{"x":"2023-04-01","y":4.8123},{"x":"2023-04-02","y":5.5642},{"x":"2023-04-03","y":5.2656},{"x":"2023-04-04","y":8.7167},{"x":"2023-04-05","y":5.3078},{"x":"2023-04-06","y":4.7283},{"x":"2023-04-07","y":4.1322},{"x":"2023-04-08","y":5.1783},{"x":"2023-04-09","y":5.2504},{"x":"2023-04-10","y":6.7794},{"x":"2023-04-11","y":5.0806},{"x":"2023-04-12","y":4.9803},{"x":"2023-04-13","y":4.3914},{"x":"2023-04-14","y":5.4071},{"x":"2023-04-15","y":4.939},{"x":"2023-04-16","y":4.4732},{"x":"2023-04-17","y":5.0562},{"x":"2023-04-18","y":5.0911},{"x":"2023-04-19","y":4.8266},{"x":"2023-04-20","y":23.4864},{"x":"2023-04-21","y":1185.7143},{"x":"2023-04-22","y":0},{"x":"2023-04-23","y":0},{"x":"2023-04-24","y":0},{"x":"2023-04-25","y":0},{"x":"2023-04-26","y":0},{"x":"2023-04-27","y":0},{"x":"2023-04-28","y":0},{"x":"2023-04-29","y":0},{"x":"2023-04-30","y":0}]}]

console.log({
  id: 'average',
  data: arr[0].data.map(({x})=>({
      x,
      y: arr.map(i => i.data.find(j => j.x===x).y)
            .reduce((a,c) => a+c) / arr.length
    })
  )}
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading