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

156
Vistas
How to redact data from array?

If I have a weather data, which is an array, like this one below.

const weather = [
{date:'2022-07-18', min_temp:15, max_temp:26, type:'clouds'},
{date:'2022-07-18', min_temp:13, max_temp:27, type:'sun'},
{date:'2022-07-18', min_temp:23, max_temp:31, type:'clouds'},
{date:'2022-07-18', min_temp:19, max_temp:28, type:'rain'},

{date:'2022-07-19', min_temp:21, max_temp:25, type:'rain'},
{date:'2022-07-19', min_temp:17, max_temp:29, type:'rain'},
{date:'2022-07-19', min_temp:14, max_temp:38, type:'rain'},
{date:'2022-07-19', min_temp:9, max_temp:30, type:'clouds'}
]

How can I reduce the array, one data/date, with the min and max temperature from the same date, and type should be the most frequently type from the same date. It should look like this one below.

const weather = [
{date:'2022-07-18', min_temp=13, max_temp31, type:'clouds'},
{date:'2022-07-19', min_temp=9, max_temp:38, type:'rain'}
]
about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

You can use reduce to collect the values per date into a map, and then get the values of that map:

const weathers = [
{date:'2022-07-18', min_temp:15, max_temp:26, type:'clouds'},
{date:'2022-07-18', min_temp:13, max_temp:27, type:'sun'},
{date:'2022-07-18', min_temp:23, max_temp:31, type:'clouds'},
{date:'2022-07-18', min_temp:19, max_temp:28, type:'rain'},
{date:'2022-07-19', min_temp:21, max_temp:25, type:'rain'},
{date:'2022-07-19', min_temp:17, max_temp:29, type:'rain'},
{date:'2022-07-19', min_temp:14, max_temp:38, type:'rain'},
{date:'2022-07-19', min_temp:9, max_temp:30, type:'clouds'}
]

const weatherByDate = weathers.reduce((acc, curr) => {
const date = curr.date
const weather = acc[date]

if (weather) {
    const copy = { ...weather }
    copy.min_temp = Math.min(curr.min_temp, weather.min_temp)
    copy.max_temp = Math.max(curr.max_temp, weather.max_temp)
    
    const count = (weather.typeCounts[curr.type] || 0) + 1
    copy.typeCounts[curr.type] = count
    
    if(count > weather.typeCounts.max) {
      copy.typeCounts.max = count
      copy.type = curr.type
    }
    
    acc[date] = copy
} else {
    acc[date] = { ...curr, typeCounts: { [curr.type]: 1, max: 1 } }
}

return acc
}, {})

const arr = Object.values(weatherByDate).map(({typeCounts, ...rest}) => rest)

console.log(arr)

EDIT: Edited to keep most frequent type per date

about 4 years ago · Santiago Gelvez Denunciar

0

The idea is first to group by date. This is done using the first reduce. While we do, we store the frequency of each type. and the min/max temp. Then we take the values of this object as the required array. Only a little housekeeping removing the freq helper object. And setting the type to be indeed the most frequent (amongst that date).

const weather = [
  {date:'2022-07-18', min_temp:15, max_temp:26, type:'clouds'},
  {date:'2022-07-18', min_temp:13, max_temp:27, type:'sun'},
  {date:'2022-07-18', min_temp:23, max_temp:31, type:'clouds'},
  {date:'2022-07-18', min_temp:19, max_temp:28, type:'rain'},
  {date:'2022-07-19', min_temp:21, max_temp:25, type:'rain'},
  {date:'2022-07-19', min_temp:17, max_temp:29, type:'rain'},
  {date:'2022-07-19', min_temp:14, max_temp:38, type:'rain'},
  {date:'2022-07-19', min_temp:9, max_temp:30, type:'clouds'}
];

var step1 = Object.values(weather.reduce(function(agg, item) {
  var {date, min_temp, max_temp, type} = item;
  if (agg[date]) {
    agg[date].min_temp = Math.min(agg[date].min_temp, min_temp)
    agg[date].max_temp = Math.max(agg[date].max_temp, max_temp)
  } else {
    agg[date] = item
    agg[date].freq = {}
  }
  agg[date].freq[type] = (agg[date].freq[type] || 0) + 1
  return agg;
}, {})).map(function(item) {
  var most_common = 0;
  Object.entries(item.freq).reduce(function(agg, [key, value]) {
    if (agg<value) {
      agg = value;
      most_common = key;
    }
  }, 0);
  delete item.freq;
  item.type = most_common;
  return item;
})
console.log(step1)

about 4 years ago · Santiago Gelvez 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