如何根据每个月的天数获得平均温度

How to get average temperature depending of days for each month

我试图通过此操作获得温度平均值:总温度(一个月)除以该月的天数(如果月份(xAxis 变量)存在于createdAt(响应变量))。

例如: 11 月的天数是(取决于 response.createdAt):3 12 月的天数是(取决于 response.createdAt):2

然后,将它们用于操作。

我知道我的解释可能含糊不清,所以如果您需要更精确的信息,请不要犹豫。

var response = [{
    "id": 294,
    "createdAt": "2021-12-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":4.93,\"Humidity\":90}"
},{
    "id": 294,
    "createdAt": "2021-12-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":7.93,\"Humidity\":90}"
},
{
    "id": 294,
    "createdAt": "2021-11-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":44.93,\"Humidity\":90}"
},{
    "id": 294,
    "createdAt": "2021-11-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":2.93,\"Humidity\":90}"
},
{
    "id": 294,
    "createdAt": "2021-11-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":3.93,\"Humidity\":90}"
}];
var xAxis = ['10','11','12']; // contains months (representinf as integer)
var temperatureFinal = Array(xAxis.length).fill(null);

response.forEach(function(item){
  var dateData = moment(item.createdAt).format("MM");
  xAxis.forEach(function(value){
    if(dateData == value) temperatureFinal[xAxis.indexOf(value)]+= JSON.parse(item.value).Temperature;
  });
});

temperatureFinal.map(h=>console.log(h));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

如果您跟踪每个月的计数,就可以实现这一点。那么平均值就是 运行 总和除以每个月的事件数。一个对象非常适合跟踪多个属性。

var response = [
  {
    "id": 294,
    "createdAt": "2021-12-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":4.93,\"Humidity\":90}"
  },
  {
    "id": 294,
    "createdAt": "2021-12-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":7.93,\"Humidity\":90}"
  },
  {
    "id": 294,
    "createdAt": "2021-11-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":44.93,\"Humidity\":90}"
  }, {
    "id": 294,
    "createdAt": "2021-11-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":2.93,\"Humidity\":90}"
  },
  {
    "id": 294,
    "createdAt": "2021-11-08T00:00:00",
    "zipcode": "33000",
    "value": "{\"Temperature\":3.93,\"Humidity\":90}"
  }
];
var xAxis = ['10', '11', '12']; // contains months (representing as integer)
var temperatureFinal = {};
xAxis.forEach(month => {
  temperatureFinal[month] = {
    month,
    count: 0,
    sum: 0,
    average: 0
  }
});

response.forEach(function(item) {
  var dateData = moment(item.createdAt).format("MM");
  xAxis.forEach(function(value) {
    if (dateData == value) {
      temperatureFinal[value].sum += JSON.parse(item.value).Temperature;
      temperatureFinal[value].count++;
      temperatureFinal[value].average = temperatureFinal[value].sum/temperatureFinal[value].count;
    }
  });
});

Object.values(temperatureFinal).map(h => console.log(h));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>