【问题标题】:How to get average temperature depending of days for each month如何根据每个月的天数获得平均温度
【发布时间】:2021-12-26 22:46:38
【问题描述】:

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

例如: 11 月的天数是(取决于 response.createdAt):3 十二月的天数是(取决于 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>

【问题讨论】:

    标签: javascript arrays algorithm momentjs


    【解决方案1】:

    如果您跟踪每个月的计数,您就可以做到这一点。然后平均值将只是运行总和除以每个月的事件数。一个对象非常适合跟踪多个属性。

    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>

    【讨论】:

      猜你喜欢
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 2019-11-15
      相关资源
      最近更新 更多