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

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

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 Relatório

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 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