Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda