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

268
Views
Cómo obtener la temperatura promedio en función de los días para cada mes

Estoy tratando de obtener el promedio de temperatura mediante esta operación: el total de temperatura (para un mes) dividido por la cantidad de días que hay en el mes (si el mes (variable xAxis) está presente en el createdAt (de la variable de respuesta) )).

Por ejemplo: el número de día de noviembre es (según la respuesta.creado en): 3 El número de día de diciembre es (según la respuesta.creado en): 2

Y luego, úsalos para la operación.

Sé que mi explicación puede ser confusa, así que no dudes si necesitas más precisión.

 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

Puede lograr esto si realiza un seguimiento del recuento de cada mes. Entonces, el promedio será solo la suma acumulada dividida por el recuento de eventos de cada mes. Un objeto se adapta bien al seguimiento de varias propiedades.

 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!