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

216
Vistas
Find average number from an array of objects based from a string value

I have an array of objects like this:

const test = [
{
 "id": "1"
 "date": "2022-03-11 21:00:00"
 "temp": "200",
 "desc": "light clouds"
},
{
 "id": "2"
 "date": "2022-03-11 22:00:00"
 "temp": "220",
 "desc": "light clouds"
},
{
 "id": "3"
 "date": "2022-03-11 23:00:00"
 "temp": "205",
 "desc": "rainy clouds"
},
{
 "id": "4"
 "date": "2022-03-12 21:00:00"
 "temp": "180",
 "desc": "light clouds"
},
{
 "id": "5"
 "date": "2022-03-12 22:00:00"
 "temp": "200",
 "desc": "rainy"
},
{
 "id": "6"
 "date": "2022-03-12 23:00:00"
 "temp": "200",
 "desc": "rainy"
},
]

What I want to do is to map over these objects and find the average temp for each day with the same date. And then also find the desc which appears the most times for that date too.

I am not really sure where to even begin, I am guessing I will have to use a map() function and then .find() but unsure of the syntax.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do it with one reduce method:

const test = [
  { "id": "1","date": "2022-03-11 21:00:00", "temp": "200", "desc": "light clouds" },
  { "id": "2", "date": "2022-03-11 22:00:00", "temp": "220", "desc": "light clouds" },
  {  "id": "3", "date": "2022-03-11 23:00:00", "temp": "205", "desc": "rainy clouds" },
  { "id": "4", "date": "2022-03-12 21:00:00", "temp": "180","desc": "light clouds"},
  {"id": "5", "date": "2022-03-12 22:00:00", "temp": "200","desc": "rainy"},
  { "id": "6", "date": "2022-03-12 23:00:00",  "temp": "200", "desc": "rainy" },
];

const result = test.reduce((accumulator, currentItem) => {
  const day = currentItem.date.substring(0, 10);
  if(!accumulator[day]){
    accumulator[day] = {
      count: 1,
      total_temp: parseInt(currentItem.temp),
      descriptions:[{count:1, description: currentItem.desc}]
    }
  } else {
    accumulator[day].count += 1;
    accumulator[day].total_temp +=  parseInt(currentItem.temp);
    const index = accumulator[day].descriptions.findIndex((item)=> item.description === currentItem.desc);
    if(index !== -1) {
       accumulator[day].descriptions[index].count += 1;
    } else {
      accumulator[day].descriptions.push({count:1, description: currentItem.desc});
    }
  }
  accumulator[day].average_temp = accumulator[day].total_temp/accumulator[day].count;
  return accumulator;
}, {});

console.log(result)

about 4 years ago · Juan Pablo Isaza 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