Home > Blockchain >  Efficient jQuery / JavaScript code to calculate average values in array of objects
Efficient jQuery / JavaScript code to calculate average values in array of objects

Time:02-12

I have a large JavaScript array of objects. Each object contains temperature data for each month for many years. I would like to find the average temperature data for each temperature-type for each month (there are actually 13 months in my data intentionally, so I want averages for 13 months).

In this jsfiddle, I have been playing with a very small subset of the data:

const array1 = 
[
    {
        "Year": 1980,
        "Month": 0,
        "Tmin": null,
        "Tmean": 11.1914,
        "Tmax": null,
        "Td": 7.01668,
        "RHmean": 76.1544,
        "RHmax": 61.7298,
        "WBGTmin": 8.3829,
        "WBGTmean": 9.75617,
        "WBGTmax": null,
        "WBGTmaxSun": null,
        "UTCImin": 9.41549,
        "UTCImean": 11.5612,
        "UTCImax": null,
        "HImin": 8.03456,
        "HImean": 10.9305,
        "HImax": 14.2173
    },
    {
        "Year": 1980,
        "Month": 1,
        "Tmin": 11.931,
        "Tmean": 15.0778,
        "Tmax": 18.1379,
        "Td": 10.4667,
        "RHmean": 74.6458,
        "RHmax": 61.0794,
        "WBGTmin": 11.3724,
        "WBGTmean": 13.3417,
        "WBGTmax": 15.1419,
        "WBGTmaxSun": null,
        "UTCImin": 12.7629,
        "UTCImean": 15.5132,
        "UTCImax": 18.0733,
        "HImin": 11.5267,
        "HImean": 14.5908,
        "HImax": 17.6032
    },
    {
        "Year": 1980,
        "Month": 2,
        "Tmin": 10.8621,
        "Tmean": 14.3429,
        "Tmax": 18.1379,
        "Td": 9.55555,
        "RHmean": 73.3624,
        "RHmax": 58.0879,
        "WBGTmin": 10.4187,
        "WBGTmean": 12.5424,
        "WBGTmax": 14.8529,
        "WBGTmaxSun": null,
        "UTCImin": 11.663,
        "UTCImean": 14.6836,
        "UTCImax": 17.9196,
        "HImin": 10.3846,
        "HImean": 13.7489,
        "HImax": 17.5251
    },
    {
        "Year": 1980,
        "Month": 3,
        "Tmin": 11.069,
        "Tmean": 13.7278,
        "Tmax": 17.3103,
        "Td": 9.47038,
        "RHmean": 76.4473,
        "RHmax": 61.5543,
        "WBGTmin": 10.6146,
        "WBGTmean": 12.1394,
        "WBGTmax": 14.3354,
        "WBGTmaxSun": null,
        "UTCImin": 11.8356,
        "UTCImean": 14.1294,
        "UTCImax": 17.2034,
        "HImin": 10.57,
        "HImean": 13.1527,
        "HImax": 16.7052
    },
    {
        "Year": 1980,
        "Month": 4,
        "Tmin": 9.37931,
        "Tmean": 12.9241,
        "Tmax": 16.2759,
        "Td": 8.23148,
        "RHmean": 74.3115,
        "RHmax": 59.5891,
        "WBGTmin": 9.04303,
        "WBGTmean": 11.2406,
        "WBGTmax": 13.2503,
        "WBGTmaxSun": null,
        "UTCImin": 10.0756,
        "UTCImean": 13.2498,
        "UTCImax": 16.0892,
        "HImin": 8.72126,
        "HImean": 12.2128,
        "HImax": 15.5158
    },
    {
        "Year": 1980,
        "Month": 5,
        "Tmin": 8.07407,
        "Tmean": 10.6399,
        "Tmax": 14.4444,
        "Td": null,
        "RHmean": 76.2411,
        "RHmax": 60.2131,
        "WBGTmin": 7.57442,
        "WBGTmean": null,
        "WBGTmax": null,
        "WBGTmaxSun": null,
        "UTCImin": 8.70486,
        "UTCImean": null,
        "UTCImax": null,
        "HImin": 7.28312,
        "HImean": 9.75634,
        "HImax": 13.4938
    },
    {
        "Year": 1980,
        "Month": 7,
        "Tmin": 3.87097,
        "Tmean": 6.74194,
        "Tmax": 10.8065,
        "Td": 2.06452,
        "RHmean": 72.9362,
        "RHmax": 55.8054,
        "WBGTmin": 3.4279,
        "WBGTmean": 5.32629,
        "WBGTmax": 8.05513,
        "WBGTmaxSun": null,
        "UTCImin": 4.16556,
        "UTCImean": 6.88706,
        "UTCImax": 10.5796,
        "HImin": 2.5963,
        "HImean": 5.3759,
        "HImax": 9.3998
    },
    {
        "Year": 1980,
        "Month": 9,
        "Tmin": 6.23333,
        "Tmean": 8.97223,
        "Tmax": 11.6,
        "Td": 6.1037,
        "RHmean": 82.351,
        "RHmax": 69.5854,
        "WBGTmin": 6.31845,
        "WBGTmean": 8.00618,
        "WBGTmax": 9.71308,
        "WBGTmaxSun": null,
        "UTCImin": 6.92209,
        "UTCImean": 9.45113,
        "UTCImax": 11.7794,
        "HImin": 5.4295,
        "HImean": 8.07536,
        "HImax": 10.6328
    },
    {
        "Year": 1980,
        "Month": 10,
        "Tmin": 8.43333,
        "Tmean": 11.2852,
        "Tmax": 13.6667,
        "Td": 7.64815,
        "RHmean": 78.9403,
        "RHmax": 67.924,
        "WBGTmin": 8.29363,
        "WBGTmean": 10.0097,
        "WBGTmax": 11.5281,
        "WBGTmaxSun": null,
        "UTCImin": 9.19437,
        "UTCImean": 11.7425,
        "UTCImax": 13.8014,
        "HImin": 7.76501,
        "HImean": 10.5307,
        "HImax": 12.863
    },
    {
        "Year": 1981,
        "Month": 0,
        "Tmin": 8.54919,
        "Tmean": 11.5048,
        "Tmax": 14.5909,
        "Td": 7.70727,
        "RHmean": 79.2693,
        "RHmax": 65.5782,
        "WBGTmin": 8.81321,
        "WBGTmean": 10.2214,
        "WBGTmax": 12.1636,
        "WBGTmaxSun": 13.6712,
        "UTCImin": 9.60639,
        "UTCImean": 11.9768,
        "UTCImax": 14.6651,
        "HImin": 8.13911,
        "HImean": 11.1262,
        "HImax": 14.1329
    },
    {
        "Year": 1981,
        "Month": 1,
        "Tmin": 10.9355,
        "Tmean": 14.6649,
        "Tmax": 18.1935,
        "Td": 12.0968,
        "RHmean": 84.7151,
        "RHmax": 67.9045,
        "WBGTmin": 11.7611,
        "WBGTmean": 13.6596,
        "WBGTmax": 15.7362,
        "WBGTmaxSun": 18.1962,
        "UTCImin": 12.2129,
        "UTCImean": 15.4285,
        "UTCImax": 18.4067,
        "HImin": 10.6675,
        "HImean": 14.3994,
        "HImax": 17.8426
    },
    {
        "Year": 1981,
        "Month": 2,
        "Tmin": 12.0741,
        "Tmean": 14.5041,
        "Tmax": 17.5185,
        "Td": 12.251,
        "RHmean": 86.5258,
        "RHmax": 72.0491,
        "WBGTmin": 12.3043,
        "WBGTmean": 13.6124,
        "WBGTmax": 15.4261,
        "WBGTmaxSun": 17.545,
        "UTCImin": 13.2307,
        "UTCImean": 15.3403,
        "UTCImax": 17.8944,
        "HImin": 11.8769,
        "HImean": 14.2698,
        "HImax": 17.2081
    },
    {
        "Year": 1981,
        "Month": 3,
        "Tmin": 12.4074,
        "Tmean": 15.4167,
        "Tmax": 18.2593,
        "Td": 12.6071,
        "RHmean": 83.9611,
        "RHmax": 70.3348,
        "WBGTmin": 12.668,
        "WBGTmean": 14.3096,
        "WBGTmax": 15.9688,
        "WBGTmaxSun": 17.5687,
        "UTCImin": 13.5998,
        "UTCImean": 16.1949,
        "UTCImax": 18.5757,
        "HImin": 12.2142,
        "HImean": 15.2067,
        "HImax": 17.9783
    },
    {
        "Year": 1981,
        "Month": 4,
        "Tmin": 10,
        "Tmean": 12.7361,
        "Tmax": 15.8571,
        "Td": 9.64087,
        "RHmean": 82.5259,
        "RHmax": 67.9274,
        "WBGTmin": 10.0824,
        "WBGTmean": 11.6051,
        "WBGTmax": 13.5108,
        "WBGTmaxSun": 14.592,
        "UTCImin": 10.9405,
        "UTCImean": 13.3256,
        "UTCImax": 15.999,
        "HImin": 9.51275,
        "HImean": 12.2205,
        "HImax": 15.2729
    },
    {
        "Year": 1981,
        "Month": 5,
        "Tmin": 7.77419,
        "Tmean": 10.0627,
        "Tmax": 13.6129,
        "Td": 5.78494,
        "RHmean": 75.9491,
        "RHmax": 61.0582,
        "WBGTmin": 7.20217,
        "WBGTmean": 8.64684,
        "WBGTmax": 10.9509,
        "WBGTmaxSun": 11.6187,
        "UTCImin": 8.33175,
        "UTCImean": 10.4036,
        "UTCImax": 13.5211,
        "HImin": 6.89413,
        "HImean": 9.10776,
        "HImax": 12.6245
    },
    {
        "Year": 1981,
        "Month": 7,
        "Tmin": 4.9,
        "Tmean": 7.17407,
        "Tmax": 10.3667,
        "Td": 2.70556,
        "RHmean": 74.8556,
        "RHmax": 61.1368,
        "WBGTmin": 4.34986,
        "WBGTmean": 5.8641,
        "WBGTmax": 7.98966,
        "WBGTmaxSun": 8.615,
        "UTCImin": 5.264,
        "UTCImean": 7.39827,
        "UTCImax": 10.2977,
        "HImin": 3.69173,
        "HImean": 5.90138,
        "HImax": 9.05522
    },
    {
        "Year": 1981,
        "Month": 8,
        "Tmin": 4.45161,
        "Tmean": 7.92652,
        "Tmax": 11.871,
        "Td": 0.390681,
        "RHmean": 61.2032,
        "RHmax": 47.431,
        "WBGTmin": 3.40579,
        "WBGTmean": 5.77148,
        "WBGTmax": 8.40882,
        "WBGTmaxSun": 9.3788,
        "UTCImin": 4.54988,
        "UTCImean": 7.83204,
        "UTCImax": 11.4088,
        "HImin": 2.9492,
        "HImean": 6.37245,
        "HImax": 10.352
    },
    {
        "Year": 1981,
        "Month": 9,
        "Tmin": 4.2,
        "Tmean": 8.3537,
        "Tmax": 10.8667,
        "Td": 5.32407,
        "RHmean": 81.6902,
        "RHmax": 69.417,
        "WBGTmin": 5.1251,
        "WBGTmean": 7.35119,
        "WBGTmax": 9.0068,
        "WBGTmaxSun": 10.4248,
        "UTCImin": 4.87588,
        "UTCImean": 8.78665,
        "UTCImax": 11.0342,
        "HImin": 3.20796,
        "HImean": 7.37767,
        "HImax": 9.82164
    },
    {
        "Year": 1981,
        "Month": 10,
        "Tmin": 7.48387,
        "Tmean": 11.0681,
        "Tmax": 13.6774,
        "Td": 7.32616,
        "RHmean": 78.8369,
        "RHmax": 67.1361,
        "WBGTmin": 7.72401,
        "WBGTmean": 9.77781,
        "WBGTmax": 11.4299,
        "WBGTmaxSun": 13.2976,
        "UTCImin": 8.26469,
        "UTCImean": 11.5077,
        "UTCImax": 13.7745,
        "HImin": 6.74846,
        "HImean": 10.2892,
        "HImax": 12.8542
    },
    {
        "Year": 1981,
        "Month": 11,
        "Tmin": 10.1379,
        "Tmean": 13.2088,
        "Tmax": 16.0345,
        "Td": 9.78353,
        "RHmean": 80.1912,
        "RHmax": 67.215,
        "WBGTmin": 10.1662,
        "WBGTmean": 11.9142,
        "WBGTmax": 13.6553,
        "WBGTmaxSun": 15.937,
        "UTCImin": 11.0545,
        "UTCImean": 13.74,
        "UTCImax": 16.1454,
        "HImin": 9.6793,
        "HImean": 12.6796,
        "HImax": 15.4494
    },
    {
        "Year": 1981,
        "Month": 12,
        "Tmin": 12.3262,
        "Tmean": 14.8907,
        "Tmax": 17.3871,
        "Td": 11.7276,
        "RHmean": 81.5082,
        "RHmax": 69.7501,
        "WBGTmin": 12.1564,
        "WBGTmean": 13.6467,
        "WBGTmax": 15.1406,
        "WBGTmaxSun": 17.6416,
        "UTCImin": 13.3456,
        "UTCImean": 15.5432,
        "UTCImax": 17.6521,
        "HImin": 12.0881,
        "HImean": 14.5641,
        "HImax": 17.0036
    },
    {
        "Year": 1982,
        "Month": 0,
        "Tmin": 7.17557,
        "Tmean": 11.1063,
        "Tmax": 14.9062,
        "Td": 6.62961,
        "RHmean": 74.9441,
        "RHmax": 59.1579,
        "WBGTmin": 7.3061,
        "WBGTmean": 9.60836,
        "WBGTmax": 12.0246,
        "WBGTmaxSun": 13.5322,
        "UTCImin": 7.93259,
        "UTCImean": 11.4494,
        "UTCImax": 14.7667,
        "HImin": 6.40302,
        "HImean": 10.2599,
        "HImax": 14.0271
    },
    {
        "Year": 1982,
        "Month": 1,
        "Tmin": 10.3389,
        "Tmean": 14.1222,
        "Tmax": 17.5019,
        "Td": 10.3722,
        "RHmean": 78.3829,
        "RHmax": 63.6444,
        "WBGTmin": 10.5689,
        "WBGTmean": 12.6951,
        "WBGTmax": 14.7305,
        "WBGTmaxSun": 17.1905,
        "UTCImin": 11.3631,
        "UTCImean": 14.632,
        "UTCImax": 17.5026,
        "HImin": 9.92938,
        "HImean": 13.6372,
        "HImax": 16.9704
    },
    {
        "Year": 1982,
        "Month": 2,
        "Tmin": 12.248,
        "Tmean": 15.379,
        "Tmax": 18.9484,
        "Td": 12.2222,
        "RHmean": 81.5975,
        "RHmax": 65.3673,
        "WBGTmin": 12.3504,
        "WBGTmean": 14.1349,
        "WBGTmax": 16.2461,
        "WBGTmaxSun": 18.365,
        "UTCImin": 13.3921,
        "UTCImean": 16.0678,
        "UTCImax": 19.0792,
        "HImin": 12.0642,
        "HImean": 15.1036,
        "HImax": 18.6068
    },
    {
        "Year": 1982,
        "Month": 3,
        "Tmin": 10.6264,
        "Tmean": 14.567,
        "Tmax": 18.228,
        "Td": 10.5268,
        "RHmean": 77.2457,
        "RHmax": 61.7686,
        "WBGTmin": 10.8023,
        "WBGTmean": 13.0499,
        "WBGTmax": 15.2515,
        "WBGTmaxSun": 16.8514,
        "UTCImin": 11.6506,
        "UTCImean": 15.059,
        "UTCImax": 18.1618,
        "HImin": 10.2382,
        "HImean": 14.0968,
        "HImax": 17.7203
    },
    {
        "Year": 1982,
        "Month": 4,
        "Tmin": 8.28927,
        "Tmean": 12.3774,
        "Tmax": 16.6935,
        "Td": 7.12836,
        "RHmean": 71.6036,
        "RHmax": 54.429,
        "WBGTmin": 8.07749,
        "WBGTmean": 10.5633,
        "WBGTmax": 13.249,
        "WBGTmaxSun": 14.3302,
        "UTCImin": 9.00788,
        "UTCImean": 12.6247,
        "UTCImax": 16.3256,
        "HImin": 7.55299,
        "HImean": 11.5407,
        "HImax": 15.8404
    },
    {
        "Year": 1982,
        "Month": 5,
        "Tmin": 6.78927,
        "Tmean": 10.3755,
        "Tmax": 14.2874,
        "Td": 6.6705,
        "RHmean": 78.6915,
        "RHmax": 61.921,
        "WBGTmin": 7.05394,
        "WBGTmean": 9.11994,
        "WBGTmax": 11.6353,
        "WBGTmaxSun": 12.3031,
        "UTCImin": 7.54962,
        "UTCImean": 10.7974,
        "UTCImax": 14.2113,
        "HImin": 5.98517,
        "HImean": 9.52347,
        "HImax": 13.389
    },
    {
        "Year": 1982,
        "Month": 6,
        "Tmin": 4.53704,
        "Tmean": 8.02778,
        "Tmax": 12.8556,
        "Td": 1.36296,
        "RHmean": 63.9868,
        "RHmax": 46.6503,
        "WBGTmin": 3.75934,
        "WBGTmean": 6.06368,
        "WBGTmax": 9.27825,
        "WBGTmaxSun": 9.8104,
        "UTCImin": 4.75433,
        "UTCImean": 8.01639,
        "UTCImax": 12.3611,
        "HImin": 3.1271,
        "HImean": 6.55658,
        "HImax": 11.4148
    },
    {
        "Year": 1982,
        "Month": 7,
        "Tmin": 3.00896,
        "Tmean": 6.69176,
        "Tmax": 11.5233,
        "Td": 0.281363,
        "RHmean": 65.0132,
        "RHmax": 47.4322,
        "WBGTmin": 2.50418,
        "WBGTmean": 4.87147,
        "WBGTmax": 8.14736,
        "WBGTmaxSun": 8.7727,
        "UTCImin": 3.12808,
        "UTCImean": 6.64994,
        "UTCImax": 11.0682,
        "HImin": 1.4728,
        "HImean": 5.11363,
        "HImax": 9.96959
    },
    {
        "Year": 1982,
        "Month": 8,
        "Tmin": 3.89074,
        "Tmean": 8.25926,
        "Tmax": 11.8352,
        "Td": 4.6963,
        "RHmean": 78.8587,
        "RHmax": 62.6882,
        "WBGTmin": 4.70963,
        "WBGTmean": 7.12274,
        "WBGTmax": 9.46413,
        "WBGTmaxSun": 10.4341,
        "UTCImin": 4.53797,
        "UTCImean": 8.6311,
        "UTCImax": 11.8118,
        "HImin": 2.82464,
        "HImean": 7.1998,
        "HImax": 10.7114
    },
    {
        "Year": 1982,
        "Month": 9,
        "Tmin": 5.52778,
        "Tmean": 9.90556,
        "Tmax": 13.4352,
        "Td": 5.8963,
        "RHmean": 76.7366,
        "RHmax": 61.2182,
        "WBGTmin": 5.94218,
        "WBGTmean": 8.55935,
        "WBGTmax": 10.8363,
        "WBGTmaxSun": 12.2543,
        "UTCImin": 6.25308,
        "UTCImean": 10.2701,
        "UTCImax": 13.3514,
        "HImin": 4.66327,
        "HImean": 8.95547,
        "HImax": 12.4332
    },
    {
        "Year": 1982,
        "Month": 10,
        "Tmin": 4.80159,
        "Tmean": 10.1171,
        "Tmax": 13.8651,
        "Td": 4.42659,
        "RHmean": 70.1723,
        "RHmax": 55.4714,
        "WBGTmin": 5.06719,
        "WBGTmean": 8.30009,
        "WBGTmax": 10.719,
        "WBGTmaxSun": 12.5867,
        "UTCImin": 5.37704,
        "UTCImean": 10.2803,
        "UTCImax": 13.5843,
        "HImin": 3.73974,
        "HImean": 9.01666,
        "HImax": 12.756
    },
    {
        "Year": 1982,
        "Month": 11,
        "Tmin": 8.21839,
        "Tmean": 11.5766,
        "Tmax": 14.4866,
        "Td": 7.87931,
        "RHmean": 78.7804,
        "RHmax": 65.8273,
        "WBGTmin": 8.29848,
        "WBGTmean": 10.2893,
        "WBGTmax": 12.1182,
        "WBGTmaxSun": 14.3999,
        "UTCImin": 9.03572,
        "UTCImean": 12.0431,
        "UTCImax": 14.5568,
        "HImin": 7.56246,
        "HImean": 10.8471,
        "HImax": 13.7102
    },
    {
        "Year": 1982,
        "Month": 12,
        "Tmin": 8.25269,
        "Tmean": 12.2079,
        "Tmax": 15.5358,
        "Td": 8.49641,
        "RHmean": 78.2601,
        "RHmax": 63.4765,
        "WBGTmin": 8.53918,
        "WBGTmean": 10.8732,
        "WBGTmax": 12.944,
        "WBGTmaxSun": 15.445,
        "UTCImin": 9.14154,
        "UTCImean": 12.6763,
        "UTCImax": 15.5241,
        "HImin": 7.67627,
        "HImean": 11.528,
        "HImax": 14.8032
    }
];

function averages({avg, n}, tempInfo) {
    if (tempInfo.WBGTmaxSun == null) {
        return {avg, n};
    }
       
   
    return {
        avg: (tempInfo.WBGTmaxSun   n * avg) / (n   1),
        n:   n   1,
    };
}

// Calculations
// ----------------------------------------------------------------------------

// Calculate the average and display.
const initialVals       = {avg: 0, n: 0};
const averageVals = array1.reduce(averages, initialVals).avg;
console.log("Average:", averageVals);

//PSEUDOCODE:
// for each month:
//      for each tempType:
//          call array1.reduce(averages, initialVals).avg;

var avgx = array1
  .filter((x) => {
      return x.Month === 1;
  })
  .map(function(obj) {return obj.WBGTmaxSun})
  .reduce(function(a, b) { return a   b });
  
console.log("Average:", avgx);

I have explored the functions reduce(), map(), and filter().
I managed to get the code to work for all months, but for only one temperature-type (WBGTmaxSun).

However, the result I want is an array containing all temperature-types, with each temperature-type having an array of temperatures for each month. Here is a screenshot of the output of my existing code, however the code is inefficient and doesn't ignore nulls in the calculations:

enter image description here

In the original array of objects, some temperature-type properties have null values, so they should not influence the average calculations.

I was hoping to use my current code in the jsfiddle inside 2 loops. Here is some pseduocode:

for each month:
    for each tempType:
        call array1.reduce(averages, initialVals).avg;

However, I don't know how to pass parameters for the month and temperature-type into the averages() function.

I could use multiple loops to achieve all of this, but I want to write efficient code. I have studied this page for assistance: FUNCTIONAL JAVASCRIPT, however I've hit a wall.

CodePudding user response:

Loop over your object, adding entries to the result object for each type and month when first encountered. Otherwise just add the result to the existing type and month in the result object.

let result = {};

array1.forEach(({Year, Month, ...temps}) => {
  Object.entries(temps).forEach(([type, temp]) => {
    if (temp === null) { // skip null values
      return;
    }
    // Add entry for temperature type of it doesn't exist
    if (!(type in result)) {
      result[type] = [];
    }
    // Use month as array index within type
    if (result[type][Month]) {
      result[type][Month].push(temp);
    } else {
      result[type][Month] = [temp];
    }
  });
});

const array1 = [{
    "Year": 1980,
    "Month": 0,
    "Tmin": null,
    "Tmean": 11.1914,
    "Tmax": null,
    "Td": 7.01668,
    "RHmean": 76.1544,
    "RHmax": 61.7298,
    "WBGTmin": 8.3829,
    "WBGTmean": 9.75617,
    "WBGTmax": null,
    "WBGTmaxSun": null,
    "UTCImin": 9.41549,
    "UTCImean": 11.5612,
    "UTCImax": null,
    "HImin": 8.03456,
    "HImean": 10.9305,
    "HImax": 14.2173
  },
  {
    "Year": 1980,
    "Month": 1,
    "Tmin": 11.931,
    "Tmean": 15.0778,
    "Tmax": 18.1379,
    "Td": 10.4667,
    "RHmean": 74.6458,
    "RHmax": 61.0794,
    "WBGTmin": 11.3724,
    "WBGTmean": 13.3417,
    "WBGTmax": 15.1419,
    "WBGTmaxSun": null,
    "UTCImin": 12.7629,
    "UTCImean": 15.5132,
    "UTCImax": 18.0733,
    "HImin": 11.5267,
    "HImean": 14.5908,
    "HImax": 17.6032
  },
  {
    "Year": 1980,
    "Month": 2,
    "Tmin": 10.8621,
    "Tmean": 14.3429,
    "Tmax": 18.1379,
    "Td": 9.55555,
    "RHmean": 73.3624,
    "RHmax": 58.0879,
    "WBGTmin": 10.4187,
    "WBGTmean": 12.5424,
    "WBGTmax": 14.8529,
    "WBGTmaxSun": null,
    "UTCImin": 11.663,
    "UTCImean": 14.6836,
    "UTCImax": 17.9196,
    "HImin": 10.3846,
    "HImean": 13.7489,
    "HImax": 17.5251
  },
  {
    "Year": 1980,
    "Month": 3,
    "Tmin": 11.069,
    "Tmean": 13.7278,
    "Tmax": 17.3103,
    "Td": 9.47038,
    "RHmean": 76.4473,
    "RHmax": 61.5543,
    "WBGTmin": 10.6146,
    "WBGTmean": 12.1394,
    "WBGTmax": 14.3354,
    "WBGTmaxSun": null,
    "UTCImin": 11.8356,
    "UTCImean": 14.1294,
    "UTCImax": 17.2034,
    "HImin": 10.57,
    "HImean": 13.1527,
    "HImax": 16.7052
  },
  {
    "Year": 1980,
    "Month": 4,
    "Tmin": 9.37931,
    "Tmean": 12.9241,
    "Tmax": 16.2759,
    "Td": 8.23148,
    "RHmean": 74.3115,
    "RHmax": 59.5891,
    "WBGTmin": 9.04303,
    "WBGTmean": 11.2406,
    "WBGTmax": 13.2503,
    "WBGTmaxSun": null,
    "UTCImin": 10.0756,
    "UTCImean": 13.2498,
    "UTCImax": 16.0892,
    "HImin": 8.72126,
    "HImean": 12.2128,
    "HImax": 15.5158
  },
  {
    "Year": 1980,
    "Month": 5,
    "Tmin": 8.07407,
    "Tmean": 10.6399,
    "Tmax": 14.4444,
    "Td": null,
    "RHmean": 76.2411,
    "RHmax": 60.2131,
    "WBGTmin": 7.57442,
    "WBGTmean": null,
    "WBGTmax": null,
    "WBGTmaxSun": null,
    "UTCImin": 8.70486,
    "UTCImean": null,
    "UTCImax": null,
    "HImin": 7.28312,
    "HImean": 9.75634,
    "HImax": 13.4938
  },
  {
    "Year": 1980,
    "Month": 7,
    "Tmin": 3.87097,
    "Tmean": 6.74194,
    "Tmax": 10.8065,
    "Td": 2.06452,
    "RHmean": 72.9362,
    "RHmax": 55.8054,
    "WBGTmin": 3.4279,
    "WBGTmean": 5.32629,
    "WBGTmax": 8.05513,
    "WBGTmaxSun": null,
    "UTCImin": 4.16556,
    "UTCImean": 6.88706,
    "UTCImax": 10.5796,
    "HImin": 2.5963,
    "HImean": 5.3759,
    "HImax": 9.3998
  },
  {
    "Year": 1980,
    "Month": 9,
    "Tmin": 6.23333,
    "Tmean": 8.97223,
    "Tmax": 11.6,
    "Td": 6.1037,
    "RHmean": 82.351,
    "RHmax": 69.5854,
    "WBGTmin": 6.31845,
    "WBGTmean": 8.00618,
    "WBGTmax": 9.71308,
    "WBGTmaxSun": null,
    "UTCImin": 6.92209,
    "UTCImean": 9.45113,
    "UTCImax": 11.7794,
    "HImin": 5.4295,
    "HImean": 8.07536,
    "HImax": 10.6328
  },
  {
    "Year": 1980,
    "Month": 10,
    "Tmin": 8.43333,
    "Tmean": 11.2852,
    "Tmax": 13.6667,
    "Td": 7.64815,
    "RHmean": 78.9403,
    "RHmax": 67.924,
    "WBGTmin": 8.29363,
    "WBGTmean": 10.0097,
    "WBGTmax": 11.5281,
    "WBGTmaxSun": null,
    "UTCImin": 9.19437,
    "UTCImean": 11.7425,
    "UTCImax": 13.8014,
    "HImin": 7.76501,
    "HImean": 10.5307,
    "HImax": 12.863
  },
  {
    "Year": 1981,
    "Month": 0,
    "Tmin": 8.54919,
    "Tmean": 11.5048,
    "Tmax": 14.5909,
    "Td": 7.70727,
    "RHmean": 79.2693,
    "RHmax": 65.5782,
    "WBGTmin": 8.81321,
    "WBGTmean": 10.2214,
    "WBGTmax": 12.1636,
    "WBGTmaxSun": 13.6712,
    "UTCImin": 9.60639,
    "UTCImean": 11.9768,
    "UTCImax": 14.6651,
    "HImin": 8.13911,
    "HImean": 11.1262,
    "HImax": 14.1329
  },
  {
    "Year": 1981,
    "Month": 1,
    "Tmin": 10.9355,
    "Tmean": 14.6649,
    "Tmax": 18.1935,
    "Td": 12.0968,
    "RHmean": 84.7151,
    "RHmax": 67.9045,
    "WBGTmin": 11.7611,
    "WBGTmean": 13.6596,
    "WBGTmax": 15.7362,
    "WBGTmaxSun": 18.1962,
    "UTCImin": 12.2129,
    "UTCImean": 15.4285,
    "UTCImax": 18.4067,
    "HImin": 10.6675,
    "HImean": 14.3994,
    "HImax": 17.8426
  },
  {
    "Year": 1981,
    "Month": 2,
    "Tmin": 12.0741,
    "Tmean": 14.5041,
    "Tmax": 17.5185,
    "Td": 12.251,
    "RHmean": 86.5258,
    "RHmax": 72.0491,
    "WBGTmin": 12.3043,
    "WBGTmean": 13.6124,
    "WBGTmax": 15.4261,
    "WBGTmaxSun": 17.545,
    "UTCImin": 13.2307,
    "UTCImean": 15.3403,
    "UTCImax": 17.8944,
    "HImin": 11.8769,
    "HImean": 14.2698,
    "HImax": 17.2081
  },
  {
    "Year": 1981,
    "Month": 3,
    "Tmin": 12.4074,
    "Tmean": 15.4167,
    "Tmax": 18.2593,
    "Td": 12.6071,
    "RHmean": 83.9611,
    "RHmax": 70.3348,
    "WBGTmin": 12.668,
    "WBGTmean": 14.3096,
    "WBGTmax": 15.9688,
    "WBGTmaxSun": 17.5687,
    "UTCImin": 13.5998,
    "UTCImean": 16.1949,
    "UTCImax": 18.5757,
    "HImin": 12.2142,
    "HImean": 15.2067,
    "HImax": 17.9783
  },
  {
    "Year": 1981,
    "Month": 4,
    "Tmin": 10,
    "Tmean": 12.7361,
    "Tmax": 15.8571,
    "Td": 9.64087,
    "RHmean": 82.5259,
    "RHmax": 67.9274,
    "WBGTmin": 10.0824,
    "WBGTmean": 11.6051,
    "WBGTmax": 13.5108,
    "WBGTmaxSun": 14.592,
    "UTCImin": 10.9405,
    "UTCImean": 13.3256,
    "UTCImax": 15.999,
    "HImin": 9.51275,
    "HImean": 12.2205,
    "HImax": 15.2729
  },
  {
    "Year": 1981,
    "Month": 5,
    "Tmin": 7.77419,
    "Tmean": 10.0627,
    "Tmax": 13.6129,
    "Td": 5.78494,
    "RHmean": 75.9491,
    "RHmax": 61.0582,
    "WBGTmin": 7.20217,
    "WBGTmean": 8.64684,
    "WBGTmax": 10.9509,
    "WBGTmaxSun": 11.6187,
    "UTCImin": 8.33175,
    "UTCImean": 10.4036,
    "UTCImax": 13.5211,
    "HImin": 6.89413,
    "HImean": 9.10776,
    "HImax": 12.6245
  },
  {
    "Year": 1981,
    "Month": 7,
    "Tmin": 4.9,
    "Tmean": 7.17407,
    "Tmax": 10.3667,
    "Td": 2.70556,
    "RHmean": 74.8556,
    "RHmax": 61.1368,
    "WBGTmin": 4.34986,
    "WBGTmean": 5.8641,
    "WBGTmax": 7.98966,
    "WBGTmaxSun": 8.615,
    "UTCImin": 5.264,
    "UTCImean": 7.39827,
    "UTCImax": 10.2977,
    "HImin": 3.69173,
    "HImean": 5.90138,
    "HImax": 9.05522
  },
  {
    "Year": 1981,
    "Month": 8,
    "Tmin": 4.45161,
    "Tmean": 7.92652,
    "Tmax": 11.871,
    "Td": 0.390681,
    "RHmean": 61.2032,
    "RHmax": 47.431,
    "WBGTmin": 3.40579,
    "WBGTmean": 5.77148,
    "WBGTmax": 8.40882,
    "WBGTmaxSun": 9.3788,
    "UTCImin": 4.54988,
    "UTCImean": 7.83204,
    "UTCImax": 11.4088,
    "HImin": 2.9492,
    "HImean": 6.37245,
    "HImax": 10.352
  },
  {
    "Year": 1981,
    "Month": 9,
    "Tmin": 4.2,
    "Tmean": 8.3537,
    "Tmax": 10.8667,
    "Td": 5.32407,
    "RHmean": 81.6902,
    "RHmax": 69.417,
    "WBGTmin": 5.1251,
    "WBGTmean": 7.35119,
    "WBGTmax": 9.0068,
    "WBGTmaxSun": 10.4248,
    "UTCImin": 4.87588,
    "UTCImean": 8.78665,
    "UTCImax": 11.0342,
    "HImin": 3.20796,
    "HImean": 7.37767,
    "HImax": 9.82164
  },
  {
    "Year": 1981,
    "Month": 10,
    "Tmin": 7.48387,
    "Tmean": 11.0681,
    "Tmax": 13.6774,
    "Td": 7.32616,
    "RHmean": 78.8369,
    "RHmax": 67.1361,
    "WBGTmin": 7.72401,
    "WBGTmean": 9.77781,
    "WBGTmax": 11.4299,
    "WBGTmaxSun": 13.2976,
    "UTCImin": 8.26469,
    "UTCImean": 11.5077,
    "UTCImax": 13.7745,
    "HImin": 6.74846,
    "HImean": 10.2892,
    "HImax": 12.8542
  },
  {
    "Year": 1981,
    "Month": 11,
    "Tmin": 10.1379,
    "Tmean": 13.2088,
    "Tmax": 16.0345,
    "Td": 9.78353,
    "RHmean": 80.1912,
    "RHmax": 67.215,
    "WBGTmin": 10.1662,
    "WBGTmean": 11.9142,
    "WBGTmax": 13.6553,
    "WBGTmaxSun": 15.937,
    "UTCImin": 11.0545,
    "UTCImean": 13.74,
    "UTCImax": 16.1454,
    "HImin": 9.6793,
    "HImean": 12.6796,
    "HImax": 15.4494
  },
  {
    "Year": 1981,
    "Month": 12,
    "Tmin": 12.3262,
    "Tmean": 14.8907,
    "Tmax": 17.3871,
    "Td": 11.7276,
    "RHmean": 81.5082,
    "RHmax": 69.7501,
    "WBGTmin": 12.1564,
    "WBGTmean": 13.6467,
    "WBGTmax": 15.1406,
    "WBGTmaxSun": 17.6416,
    "UTCImin": 13.3456,
    "UTCImean": 15.5432,
    "UTCImax": 17.6521,
    "HImin": 12.0881,
    "HImean": 14.5641,
    "HImax": 17.0036
  },
  {
    "Year": 1982,
    "Month": 0,
    "Tmin": 7.17557,
    "Tmean": 11.1063,
    "Tmax": 14.9062,
    "Td": 6.62961,
    "RHmean": 74.9441,
    "RHmax": 59.1579,
    "WBGTmin": 7.3061,
    "WBGTmean": 9.60836,
    "WBGTmax": 12.0246,
    "WBGTmaxSun": 13.5322,
    "UTCImin": 7.93259,
    "UTCImean": 11.4494,
    "UTCImax": 14.7667,
    "HImin": 6.40302,
    "HImean": 10.2599,
    "HImax": 14.0271
  },
  {
    "Year": 1982,
    "Month": 1,
    "Tmin": 10.3389,
    "Tmean": 14.1222,
    "Tmax": 17.5019,
    "Td": 10.3722,
    "RHmean": 78.3829,
    "RHmax": 63.6444,
    "WBGTmin": 10.5689,
    "WBGTmean": 12.6951,
    "WBGTmax": 14.7305,
    "WBGTmaxSun": 17.1905,
    "UTCImin": 11.3631,
    "UTCImean": 14.632,
    "UTCImax": 17.5026,
    "HImin": 9.92938,
    "HImean": 13.6372,
    "HImax": 16.9704
  },
  {
    "Year": 1982,
    "Month": 2,
    "Tmin": 12.248,
    "Tmean": 15.379,
    "Tmax": 18.9484,
    "Td": 12.2222,
    "RHmean": 81.5975,
    "RHmax": 65.3673,
    "WBGTmin": 12.3504,
    "WBGTmean": 14.1349,
    "WBGTmax": 16.2461,
    "WBGTmaxSun": 18.365,
    "UTCImin": 13.3921,
    "UTCImean": 16.0678,
    "UTCImax": 19.0792,
    "HImin": 12.0642,
    "HImean": 15.1036,
    "HImax": 18.6068
  },
  {
    "Year": 1982,
    "Month": 3,
    "Tmin": 10.6264,
    "Tmean": 14.567,
    "Tmax": 18.228,
    "Td": 10.5268,
    "RHmean": 77.2457,
    "RHmax": 61.7686,
    "WBGTmin": 10.8023,
    "WBGTmean": 13.0499,
    "WBGTmax": 15.2515,
    "WBGTmaxSun": 16.8514,
    "UTCImin": 11.6506,
    "UTCImean": 15.059,
    "UTCImax": 18.1618,
    "HImin": 10.2382,
    "HImean": 14.0968,
    "HImax": 17.7203
  },
  {
    "Year": 1982,
    "Month": 4,
    "Tmin": 8.28927,
    "Tmean": 12.3774,
    "Tmax": 16.6935,
    "Td": 7.12836,
    "RHmean": 71.6036,
    "RHmax": 54.429,
    "WBGTmin": 8.07749,
    "WBGTmean": 10.5633,
    "WBGTmax": 13.249,
    "WBGTmaxSun": 14.3302,
    "UTCImin": 9.00788,
    "UTCImean": 12.6247,
    "UTCImax": 16.3256,
    "HImin": 7.55299,
    "HImean": 11.5407,
    "HImax": 15.8404
  },
  {
    "Year": 1982,
    "Month": 5,
    "Tmin": 6.78927,
    "Tmean": 10.3755,
    "Tmax": 14.2874,
    "Td": 6.6705,
    "RHmean": 78.6915,
    "RHmax": 61.921,
    "WBGTmin": 7.05394,
    "WBGTmean": 9.11994,
    "WBGTmax": 11.6353,
    "WBGTmaxSun": 12.3031,
    "UTCImin": 7.54962,
    "UTCImean": 10.7974,
    "UTCImax": 14.2113,
    "HImin": 5.98517,
    "HImean": 9.52347,
    "HImax": 13.389
  },
  {
    "Year": 1982,
    "Month": 6,
    "Tmin": 4.53704,
    "Tmean": 8.02778,
    "Tmax": 12.8556,
    "Td": 1.36296,
    "RHmean": 63.9868,
    "RHmax": 46.6503,
    "WBGTmin": 3.75934,
    "WBGTmean": 6.06368,
    "WBGTmax": 9.27825,
    "WBGTmaxSun": 9.8104,
    "UTCImin": 4.75433,
    "UTCImean": 8.01639,
    "UTCImax": 12.3611,
    "HImin": 3.1271,
    "HImean": 6.55658,
    "HImax": 11.4148
  },
  {
    "Year": 1982,
    "Month": 7,
    "Tmin": 3.00896,
    "Tmean": 6.69176,
    "Tmax": 11.5233,
    "Td": 0.281363,
    "RHmean": 65.0132,
    "RHmax": 47.4322,
    "WBGTmin": 2.50418,
    "WBGTmean": 4.87147,
    "WBGTmax": 8.14736,
    "WBGTmaxSun": 8.7727,
    "UTCImin": 3.12808,
    "UTCImean": 6.64994,
    "UTCImax": 11.0682,
    "HImin": 1.4728,
    "HImean": 5.11363,
    "HImax": 9.96959
  },
  {
    "Year": 1982,
    "Month": 8,
    "Tmin": 3.89074,
    "Tmean": 8.25926,
    "Tmax": 11.8352,
    "Td": 4.6963,
    "RHmean": 78.8587,
    "RHmax": 62.6882,
    "WBGTmin": 4.70963,
    "WBGTmean": 7.12274,
    "WBGTmax": 9.46413,
    "WBGTmaxSun": 10.4341,
    "UTCImin": 4.53797,
    "UTCImean": 8.6311,
    "UTCImax": 11.8118,
    "HImin": 2.82464,
    "HImean": 7.1998,
    "HImax": 10.7114
  },
  {
    "Year": 1982,
    "Month": 9,
    "Tmin": 5.52778,
    "Tmean": 9.90556,
    "Tmax": 13.4352,
    "Td": 5.8963,
    "RHmean": 76.7366,
    "RHmax": 61.2182,
    "WBGTmin": 5.94218,
    "WBGTmean": 8.55935,
    "WBGTmax": 10.8363,
    "WBGTmaxSun": 12.2543,
    "UTCImin": 6.25308,
    "UTCImean": 10.2701,
    "UTCImax": 13.3514,
    "HImin": 4.66327,
    "HImean": 8.95547,
    "HImax": 12.4332
  },
  {
    "Year": 1982,
    "Month": 10,
    "Tmin": 4.80159,
    "Tmean": 10.1171,
    "Tmax": 13.8651,
    "Td": 4.42659,
    "RHmean": 70.1723,
    "RHmax": 55.4714,
    "WBGTmin": 5.06719,
    "WBGTmean": 8.30009,
    "WBGTmax": 10.719,
    "WBGTmaxSun": 12.5867,
    "UTCImin": 5.37704,
    "UTCImean": 10.2803,
    "UTCImax": 13.5843,
    "HImin": 3.73974,
    "HImean": 9.01666,
    "HImax": 12.756
  },
  {
    "Year": 1982,
    "Month": 11,
    "Tmin": 8.21839,
    "Tmean": 11.5766,
    "Tmax": 14.4866,
    "Td": 7.87931,
    "RHmean": 78.7804,
    "RHmax": 65.8273,
    "WBGTmin": 8.29848,
    "WBGTmean": 10.2893,
    "WBGTmax": 12.1182,
    "WBGTmaxSun": 14.3999,
    "UTCImin": 9.03572,
    "UTCImean": 12.0431,
    "UTCImax": 14.5568,
    "HImin": 7.56246,
    "HImean": 10.8471,
    "HImax": 13.7102
  },
  {
    "Year": 1982,
    "Month": 12,
    "Tmin": 8.25269,
    "Tmean": 12.2079,
    "Tmax": 15.5358,
    "Td": 8.49641,
    "RHmean": 78.2601,
    "RHmax": 63.4765,
    "WBGTmin": 8.53918,
    "WBGTmean": 10.8732,
    "WBGTmax": 12.944,
    "WBGTmaxSun": 15.445,
    "UTCImin": 9.14154,
    "UTCImean": 12.6763,
    "UTCImax": 15.5241,
    "HImin": 7.67627,
    "HImean": 11.528,
    "HImax": 14.8032
  }
];

function averages({
  avg,
  n
}, tempInfo) {
  if (tempInfo.WBGTmaxSun == null) {
    return {
      avg,
      n
    };
  }


  return {
    avg: (tempInfo.WBGTmaxSun   n * avg) / (n   1),
    n: n   1,
  };
}

// Calculations
// ----------------------------------------------------------------------------

let result = {};

array1.forEach(({Year, Month, ...temps}) => {
  Object.entries(temps).forEach(([type, temp]) => {
    if (temp === null) { // skip null values
      return;
    }
    // Add entry for temperature type of it doesn't exist
    if (!(type in result)) {
      result[type] = [];
    }
    // Use month as array index within type
    if (result[type][Month]) {
      result[type][Month].push(temp);
    } else {
      result[type][Month] = [temp];
    }
  });
});

console.log(result);

CodePudding user response:

I have solved this with this code:

const array1 = 
[
    {
        "Year": 1980,
        "Month": 0,
        "Tmin": null,
        "Tmean": 11.1914,
        "Tmax": null,
        "Td": 7.01668,
        "RHmean": 76.1544,
        "RHmax": 61.7298,
        "WBGTmin": 8.3829,
        "WBGTmean": 9.75617,
        "WBGTmax": null,
        "WBGTmaxSun": null,
        "UTCImin": 9.41549,
        "UTCImean": 11.5612,
        "UTCImax": null,
        "HImin": 8.03456,
        "HImean": 10.9305,
        "HImax": 14.2173
    },
    {
        "Year": 1980,
        "Month": 1,
        "Tmin": 11.931,
        "Tmean": 15.0778,
        "Tmax": 18.1379,
        "Td": 10.4667,
        "RHmean": 74.6458,
        "RHmax": 61.0794,
        "WBGTmin": 11.3724,
        "WBGTmean": 13.3417,
        "WBGTmax": 15.1419,
        "WBGTmaxSun": null,
        "UTCImin": 12.7629,
        "UTCImean": 15.5132,
        "UTCImax": 18.0733,
        "HImin": 11.5267,
        "HImean": 14.5908,
        "HImax": 17.6032
    },
    {
        "Year": 1980,
        "Month": 2,
        "Tmin": 10.8621,
        "Tmean": 14.3429,
        "Tmax": 18.1379,
        "Td": 9.55555,
        "RHmean": 73.3624,
        "RHmax": 58.0879,
        "WBGTmin": 10.4187,
        "WBGTmean": 12.5424,
        "WBGTmax": 14.8529,
        "WBGTmaxSun": null,
        "UTCImin": 11.663,
        "UTCImean": 14.6836,
        "UTCImax": 17.9196,
        "HImin": 10.3846,
        "HImean": 13.7489,
        "HImax": 17.5251
    },
    {
        "Year": 1980,
        "Month": 3,
        "Tmin": 11.069,
        "Tmean": 13.7278,
        "Tmax": 17.3103,
        "Td": 9.47038,
        "RHmean": 76.4473,
        "RHmax": 61.5543,
        "WBGTmin": 10.6146,
        "WBGTmean": 12.1394,
        "WBGTmax": 14.3354,
        "WBGTmaxSun": null,
        "UTCImin": 11.8356,
        "UTCImean": 14.1294,
        "UTCImax": 17.2034,
        "HImin": 10.57,
        "HImean": 13.1527,
        "HImax": 16.7052
    },
    {
        "Year": 1980,
        "Month": 4,
        "Tmin": 9.37931,
        "Tmean": 12.9241,
        "Tmax": 16.2759,
        "Td": 8.23148,
        "RHmean": 74.3115,
        "RHmax": 59.5891,
        "WBGTmin": 9.04303,
        "WBGTmean": 11.2406,
        "WBGTmax": 13.2503,
        "WBGTmaxSun": null,
        "UTCImin": 10.0756,
        "UTCImean": 13.2498,
        "UTCImax": 16.0892,
        "HImin": 8.72126,
        "HImean": 12.2128,
        "HImax": 15.5158
    },
    {
        "Year": 1980,
        "Month": 5,
        "Tmin": 8.07407,
        "Tmean": 10.6399,
        "Tmax": 14.4444,
        "Td": null,
        "RHmean": 76.2411,
        "RHmax": 60.2131,
        "WBGTmin": 7.57442,
        "WBGTmean": null,
        "WBGTmax": null,
        "WBGTmaxSun": null,
        "UTCImin": 8.70486,
        "UTCImean": null,
        "UTCImax": null,
        "HImin": 7.28312,
        "HImean": 9.75634,
        "HImax": 13.4938
    },
    {
        "Year": 1980,
        "Month": 7,
        "Tmin": 3.87097,
        "Tmean": 6.74194,
        "Tmax": 10.8065,
        "Td": 2.06452,
        "RHmean": 72.9362,
        "RHmax": 55.8054,
        "WBGTmin": 3.4279,
        "WBGTmean": 5.32629,
        "WBGTmax": 8.05513,
        "WBGTmaxSun": null,
        "UTCImin": 4.16556,
        "UTCImean": 6.88706,
        "UTCImax": 10.5796,
        "HImin": 2.5963,
        "HImean": 5.3759,
        "HImax": 9.3998
    },
    {
        "Year": 1980,
        "Month": 9,
        "Tmin": 6.23333,
        "Tmean": 8.97223,
        "Tmax": 11.6,
        "Td": 6.1037,
        "RHmean": 82.351,
        "RHmax": 69.5854,
        "WBGTmin": 6.31845,
        "WBGTmean": 8.00618,
        "WBGTmax": 9.71308,
        "WBGTmaxSun": null,
        "UTCImin": 6.92209,
        "UTCImean": 9.45113,
        "UTCImax": 11.7794,
        "HImin": 5.4295,
        "HImean": 8.07536,
        "HImax": 10.6328
    },
    {
        "Year": 1980,
        "Month": 10,
        "Tmin": 8.43333,
        "Tmean": 11.2852,
        "Tmax": 13.6667,
        "Td": 7.64815,
        "RHmean": 78.9403,
        "RHmax": 67.924,
        "WBGTmin": 8.29363,
        "WBGTmean": 10.0097,
        "WBGTmax": 11.5281,
        "WBGTmaxSun": null,
        "UTCImin": 9.19437,
        "UTCImean": 11.7425,
        "UTCImax": 13.8014,
        "HImin": 7.76501,
        "HImean": 10.5307,
        "HImax": 12.863
    },
    {
        "Year": 1981,
        "Month": 0,
        "Tmin": 8.54919,
        "Tmean": 11.5048,
        "Tmax": 14.5909,
        "Td": 7.70727,
        "RHmean": 79.2693,
        "RHmax": 65.5782,
        "WBGTmin": 8.81321,
        "WBGTmean": 10.2214,
        "WBGTmax": 12.1636,
        "WBGTmaxSun": 13.6712,
        "UTCImin": 9.60639,
        "UTCImean": 11.9768,
        "UTCImax": 14.6651,
        "HImin": 8.13911,
        "HImean": 11.1262,
        "HImax": 14.1329
    },
    {
        "Year": 1981,
        "Month": 1,
        "Tmin": 10.9355,
        "Tmean": 14.6649,
        "Tmax": 18.1935,
        "Td": 12.0968,
        "RHmean": 84.7151,
        "RHmax": 67.9045,
        "WBGTmin": 11.7611,
        "WBGTmean": 13.6596,
        "WBGTmax": 15.7362,
        "WBGTmaxSun": 18.1962,
        "UTCImin": 12.2129,
        "UTCImean": 15.4285,
        "UTCImax": 18.4067,
        "HImin": 10.6675,
        "HImean": 14.3994,
        "HImax": 17.8426
    },
    {
        "Year": 1981,
        "Month": 2,
        "Tmin": 12.0741,
        "Tmean": 14.5041,
        "Tmax": 17.5185,
        "Td": 12.251,
        "RHmean": 86.5258,
        "RHmax": 72.0491,
        "WBGTmin": 12.3043,
        "WBGTmean": 13.6124,
        "WBGTmax": 15.4261,
        "WBGTmaxSun": 17.545,
        "UTCImin": 13.2307,
        "UTCImean": 15.3403,
        "UTCImax": 17.8944,
        "HImin": 11.8769,
        "HImean": 14.2698,
        "HImax": 17.2081
    },
    {
        "Year": 1981,
        "Month": 3,
        "Tmin": 12.4074,
        "Tmean": 15.4167,
        "Tmax": 18.2593,
        "Td": 12.6071,
        "RHmean": 83.9611,
        "RHmax": 70.3348,
        "WBGTmin": 12.668,
        "WBGTmean": 14.3096,
        "WBGTmax": 15.9688,
        "WBGTmaxSun": 17.5687,
        "UTCImin": 13.5998,
        "UTCImean": 16.1949,
        "UTCImax": 18.5757,
        "HImin": 12.2142,
        "HImean": 15.2067,
        "HImax": 17.9783
    },
    {
        "Year": 1981,
        "Month": 4,
        "Tmin": 10,
        "Tmean": 12.7361,
        "Tmax": 15.8571,
        "Td": 9.64087,
        "RHmean": 82.5259,
        "RHmax": 67.9274,
        "WBGTmin": 10.0824,
        "WBGTmean": 11.6051,
        "WBGTmax": 13.5108,
        "WBGTmaxSun": 14.592,
        "UTCImin": 10.9405,
        "UTCImean": 13.3256,
        "UTCImax": 15.999,
        "HImin": 9.51275,
        "HImean": 12.2205,
        "HImax": 15.2729
    },
    {
        "Year": 1981,
        "Month": 5,
        "Tmin": 7.77419,
        "Tmean": 10.0627,
        "Tmax": 13.6129,
        "Td": 5.78494,
        "RHmean": 75.9491,
        "RHmax": 61.0582,
        "WBGTmin": 7.20217,
        "WBGTmean": 8.64684,
        "WBGTmax": 10.9509,
        "WBGTmaxSun": 11.6187,
        "UTCImin": 8.33175,
        "UTCImean": 10.4036,
        "UTCImax": 13.5211,
        "HImin": 6.89413,
        "HImean": 9.10776,
        "HImax": 12.6245
    },
    {
        "Year": 1981,
        "Month": 7,
        "Tmin": 4.9,
        "Tmean": 7.17407,
        "Tmax": 10.3667,
        "Td": 2.70556,
        "RHmean": 74.8556,
        "RHmax": 61.1368,
        "WBGTmin": 4.34986,
        "WBGTmean": 5.8641,
        "WBGTmax": 7.98966,
        "WBGTmaxSun": 8.615,
        "UTCImin": 5.264,
        "UTCImean": 7.39827,
        "UTCImax": 10.2977,
        "HImin": 3.69173,
        "HImean": 5.90138,
        "HImax": 9.05522
    },
    {
        "Year": 1981,
        "Month": 8,
        "Tmin": 4.45161,
        "Tmean": 7.92652,
        "Tmax": 11.871,
        "Td": 0.390681,
        "RHmean": 61.2032,
        "RHmax": 47.431,
        "WBGTmin": 3.40579,
        "WBGTmean": 5.77148,
        "WBGTmax": 8.40882,
        "WBGTmaxSun": 9.3788,
        "UTCImin": 4.54988,
        "UTCImean": 7.83204,
        "UTCImax": 11.4088,
        "HImin": 2.9492,
        "HImean": 6.37245,
        "HImax": 10.352
    },
    {
        "Year": 1981,
        "Month": 9,
        "Tmin": 4.2,
        "Tmean": 8.3537,
        "Tmax": 10.8667,
        "Td": 5.32407,
        "RHmean": 81.6902,
        "RHmax": 69.417,
        "WBGTmin": 5.1251,
        "WBGTmean": 7.35119,
        "WBGTmax": 9.0068,
        "WBGTmaxSun": 10.4248,
        "UTCImin": 4.87588,
        "UTCImean": 8.78665,
        "UTCImax": 11.0342,
        "HImin": 3.20796,
        "HImean": 7.37767,
        "HImax": 9.82164
    },
    {
        "Year": 1981,
        "Month": 10,
        "Tmin": 7.48387,
        "Tmean": 11.0681,
        "Tmax": 13.6774,
        "Td": 7.32616,
        "RHmean": 78.8369,
        "RHmax": 67.1361,
        "WBGTmin": 7.72401,
        "WBGTmean": 9.77781,
        "WBGTmax": 11.4299,
        "WBGTmaxSun": 13.2976,
        "UTCImin": 8.26469,
        "UTCImean": 11.5077,
        "UTCImax": 13.7745,
        "HImin": 6.74846,
        "HImean": 10.2892,
        "HImax": 12.8542
    },
    {
        "Year": 1981,
        "Month": 11,
        "Tmin": 10.1379,
        "Tmean": 13.2088,
        "Tmax": 16.0345,
        "Td": 9.78353,
        "RHmean": 80.1912,
        "RHmax": 67.215,
        "WBGTmin": 10.1662,
        "WBGTmean": 11.9142,
        "WBGTmax": 13.6553,
        "WBGTmaxSun": 15.937,
        "UTCImin": 11.0545,
        "UTCImean": 13.74,
        "UTCImax": 16.1454,
        "HImin": 9.6793,
        "HImean": 12.6796,
        "HImax": 15.4494
    },
    {
        "Year": 1981,
        "Month": 12,
        "Tmin": 12.3262,
        "Tmean": 14.8907,
        "Tmax": 17.3871,
        "Td": 11.7276,
        "RHmean": 81.5082,
        "RHmax": 69.7501,
        "WBGTmin": 12.1564,
        "WBGTmean": 13.6467,
        "WBGTmax": 15.1406,
        "WBGTmaxSun": 17.6416,
        "UTCImin": 13.3456,
        "UTCImean": 15.5432,
        "UTCImax": 17.6521,
        "HImin": 12.0881,
        "HImean": 14.5641,
        "HImax": 17.0036
    },
    {
        "Year": 1982,
        "Month": 0,
        "Tmin": 7.17557,
        "Tmean": 11.1063,
        "Tmax": 14.9062,
        "Td": 6.62961,
        "RHmean": 74.9441,
        "RHmax": 59.1579,
        "WBGTmin": 7.3061,
        "WBGTmean": 9.60836,
        "WBGTmax": 12.0246,
        "WBGTmaxSun": 13.5322,
        "UTCImin": 7.93259,
        "UTCImean": 11.4494,
        "UTCImax": 14.7667,
        "HImin": 6.40302,
        "HImean": 10.2599,
        "HImax": 14.0271
    },
    {
        "Year": 1982,
        "Month": 1,
        "Tmin": 10.3389,
        "Tmean": 14.1222,
        "Tmax": 17.5019,
        "Td": 10.3722,
        "RHmean": 78.3829,
        "RHmax": 63.6444,
        "WBGTmin": 10.5689,
        "WBGTmean": 12.6951,
        "WBGTmax": 14.7305,
        "WBGTmaxSun": 17.1905,
        "UTCImin": 11.3631,
        "UTCImean": 14.632,
        "UTCImax": 17.5026,
        "HImin": 9.92938,
        "HImean": 13.6372,
        "HImax": 16.9704
    },
    {
        "Year": 1982,
        "Month": 2,
        "Tmin": 12.248,
        "Tmean": 15.379,
        "Tmax": 18.9484,
        "Td": 12.2222,
        "RHmean": 81.5975,
        "RHmax": 65.3673,
        "WBGTmin": 12.3504,
        "WBGTmean": 14.1349,
        "WBGTmax": 16.2461,
        "WBGTmaxSun": 18.365,
        "UTCImin": 13.3921,
        "UTCImean": 16.0678,
        "UTCImax": 19.0792,
        "HImin": 12.0642,
        "HImean": 15.1036,
        "HImax": 18.6068
    },
    {
        "Year": 1982,
        "Month": 3,
        "Tmin": 10.6264,
        "Tmean": 14.567,
        "Tmax": 18.228,
        "Td": 10.5268,
        "RHmean": 77.2457,
        "RHmax": 61.7686,
        "WBGTmin": 10.8023,
        "WBGTmean": 13.0499,
        "WBGTmax": 15.2515,
        "WBGTmaxSun": 16.8514,
        "UTCImin": 11.6506,
        "UTCImean": 15.059,
        "UTCImax": 18.1618,
        "HImin": 10.2382,
        "HImean": 14.0968,
        "HImax": 17.7203
    },
    {
        "Year": 1982,
        "Month": 4,
        "Tmin": 8.28927,
        "Tmean": 12.3774,
        "Tmax": 16.6935,
        "Td": 7.12836,
        "RHmean": 71.6036,
        "RHmax": 54.429,
        "WBGTmin": 8.07749,
        "WBGTmean": 10.5633,
        "WBGTmax": 13.249,
        "WBGTmaxSun": 14.3302,
        "UTCImin": 9.00788,
        "UTCImean": 12.6247,
        "UTCImax": 16.3256,
        "HImin": 7.55299,
        "HImean": 11.5407,
        "HImax": 15.8404
    },
    {
        "Year": 1982,
        "Month": 5,
        "Tmin": 6.78927,
        "Tmean": 10.3755,
        "Tmax": 14.2874,
        "Td": 6.6705,
        "RHmean": 78.6915,
        "RHmax": 61.921,
        "WBGTmin": 7.05394,
        "WBGTmean": 9.11994,
        "WBGTmax": 11.6353,
        "WBGTmaxSun": 12.3031,
        "UTCImin": 7.54962,
        "UTCImean": 10.7974,
        "UTCImax": 14.2113,
        "HImin": 5.98517,
        "HImean": 9.52347,
        "HImax": 13.389
    },
    {
        "Year": 1982,
        "Month": 6,
        "Tmin": 4.53704,
        "Tmean": 8.02778,
        "Tmax": 12.8556,
        "Td": 1.36296,
        "RHmean": 63.9868,
        "RHmax": 46.6503,
        "WBGTmin": 3.75934,
        "WBGTmean": 6.06368,
        "WBGTmax": 9.27825,
        "WBGTmaxSun": 9.8104,
        "UTCImin": 4.75433,
        "UTCImean": 8.01639,
        "UTCImax": 12.3611,
        "HImin": 3.1271,
        "HImean": 6.55658,
        "HImax": 11.4148
    },
    {
        "Year": 1982,
        "Month": 7,
        "Tmin": 3.00896,
        "Tmean": 6.69176,
        "Tmax": 11.5233,
        "Td": 0.281363,
        "RHmean": 65.0132,
        "RHmax": 47.4322,
        "WBGTmin": 2.50418,
        "WBGTmean": 4.87147,
        "WBGTmax": 8.14736,
        "WBGTmaxSun": 8.7727,
        "UTCImin": 3.12808,
        "UTCImean": 6.64994,
        "UTCImax": 11.0682,
        "HImin": 1.4728,
        "HImean": 5.11363,
        "HImax": 9.96959
    },
    {
        "Year": 1982,
        "Month": 8,
        "Tmin": 3.89074,
        "Tmean": 8.25926,
        "Tmax": 11.8352,
        "Td": 4.6963,
        "RHmean": 78.8587,
        "RHmax": 62.6882,
        "WBGTmin": 4.70963,
        "WBGTmean": 7.12274,
        "WBGTmax": 9.46413,
        "WBGTmaxSun": 10.4341,
        "UTCImin": 4.53797,
        "UTCImean": 8.6311,
        "UTCImax": 11.8118,
        "HImin": 2.82464,
        "HImean": 7.1998,
        "HImax": 10.7114
    },
    {
        "Year": 1982,
        "Month": 9,
        "Tmin": 5.52778,
        "Tmean": 9.90556,
        "Tmax": 13.4352,
        "Td": 5.8963,
        "RHmean": 76.7366,
        "RHmax": 61.2182,
        "WBGTmin": 5.94218,
        "WBGTmean": 8.55935,
        "WBGTmax": 10.8363,
        "WBGTmaxSun": 12.2543,
        "UTCImin": 6.25308,
        "UTCImean": 10.2701,
        "UTCImax": 13.3514,
        "HImin": 4.66327,
        "HImean": 8.95547,
        "HImax": 12.4332
    },
    {
        "Year": 1982,
        "Month": 10,
        "Tmin": 4.80159,
        "Tmean": 10.1171,
        "Tmax": 13.8651,
        "Td": 4.42659,
        "RHmean": 70.1723,
        "RHmax": 55.4714,
        "WBGTmin": 5.06719,
        "WBGTmean": 8.30009,
        "WBGTmax": 10.719,
        "WBGTmaxSun": 12.5867,
        "UTCImin": 5.37704,
        "UTCImean": 10.2803,
        "UTCImax": 13.5843,
        "HImin": 3.73974,
        "HImean": 9.01666,
        "HImax": 12.756
    },
    {
        "Year": 1982,
        "Month": 11,
        "Tmin": 8.21839,
        "Tmean": 11.5766,
        "Tmax": 14.4866,
        "Td": 7.87931,
        "RHmean": 78.7804,
        "RHmax": 65.8273,
        "WBGTmin": 8.29848,
        "WBGTmean": 10.2893,
        "WBGTmax": 12.1182,
        "WBGTmaxSun": 14.3999,
        "UTCImin": 9.03572,
        "UTCImean": 12.0431,
        "UTCImax": 14.5568,
        "HImin": 7.56246,
        "HImean": 10.8471,
        "HImax": 13.7102
    },
    {
        "Year": 1982,
        "Month": 12,
        "Tmin": 8.25269,
        "Tmean": 12.2079,
        "Tmax": 15.5358,
        "Td": 8.49641,
        "RHmean": 78.2601,
        "RHmax": 63.4765,
        "WBGTmin": 8.53918,
        "WBGTmean": 10.8732,
        "WBGTmax": 12.944,
        "WBGTmaxSun": 15.445,
        "UTCImin": 9.14154,
        "UTCImean": 12.6763,
        "UTCImax": 15.5241,
        "HImin": 7.67627,
        "HImean": 11.528,
        "HImax": 14.8032
    }
];


const initialVals = {avg: 0, n: 0};

const tempTypes = ["Tmin", "Tmean", "Tmax","Td", "RHmean", "RHmax", "WBGTmin", "WBGTmean",  "WBGTmax","WBGTmaxSun", "UTCImin",   "UTCImean",  "UTCImax",         "HImin",  "HImean",  "HImax"];

for (var m = 0; m < 13; m  )
{
    for (var t = 0; t < tempTypes.length; t  )
    {
    var avgx = array1
    .filter((x) => {
      return x.Month === m;
    })
    .map(function(obj) {return obj[tempTypes[t]]})
    .reduce(averageCalcs, initialVals).avg;

    console.log(tempTypes[t]   ": Month: "   m   " : "   avgx);
    }
}


function averageCalcs({avg, n}, tempInfo) {
    if (tempInfo == null) {
        return {avg, n};
    }
   
    return {
        avg: (tempInfo   n * avg) / (n   1),
        n:   n   1,
    };
}
  • Related