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

153
Vistas
¿Cómo redactar datos de una matriz?

Si tengo datos meteorológicos, que es una matriz, como esta a continuación.

 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'} ]

¿Cómo puedo reducir la matriz, un dato/fecha, con la temperatura mínima y máxima de la misma fecha, y el tipo debe ser el tipo más frecuente de la misma fecha? Debería verse como este a continuación.

 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

Puede usar reduce para recopilar los valores por fecha en un mapa y luego obtener los valores de ese mapa:

 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)

EDITAR: Editado para mantener el type más frecuente por fecha

about 4 years ago · Santiago Gelvez Denunciar

0

La idea es primero agrupar por fecha. Esto se hace usando la primera reducción. Mientras lo hacemos, almacenamos la frecuencia de cada tipo. y la temperatura mínima/máxima. Luego tomamos los valores de este objeto como la matriz requerida. Solo un poco de limpieza eliminando el freq auxiliar de frecuencia. Y configurando el type para que sea efectivamente el más frecuente (entre esa fecha).

 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