Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda