Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
How to get average temperature depending of days for each month

I'm trying to get the temperature average by this operation : the total of temperature (for a month) dividing by the number of day that are in the month (if the month (xAxis variable) is present in the createdAt (of response variable)).

For example : The number of day for November is (depending of response.createdAt) : 3 The number of day for December is (depending of response.createdAt) : 2

And then, use them for the operation.

I know that my explanation might be foggy, so don't hesitate if you need more precision.

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>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can achieve this if you track the count of each month. Then the average will just be the running sum divided by the count of events for each month. An object is well suited to tracking multiple properties.

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>

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!