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

89
Visualizações
Convert an object by aggregating multiple rows in react

I have been trying to convert the following code

{
  0: {
   host: 'first'
   date: '2021-04-03'
   first_count: 12
   second_count: 10
  }
  1: {
   host: 'first'
   date: '2021-04-02'
   first_count: 10
   second_count: 8
  }
  2: {
   host: 'first'
   date: '2021-04-01'
   first_count: 23
   second_count: 30
  }
}

into

{
  0: {
    host: 'first'
    first_count_array: {
      0: {
        date: '2021-04-03'
        first_count: 12
      }
      1: {
        date: '2021-04-02'
        first_count: 10
      }
      2: {
        date: '2021-04-01'
        first_count: 23
      }
    }
    second_count_array: {
      0: {
        date: '2021-04-03'
        second_count: 10
      }
      1: {
        date: '2021-04-02'
        second_count: 8
      }
      2: {
        date: '2021-04-01'
        second_count: 30
      }
    }
  }
}

without using nested loops.

I have tried foreach loop but it involves nested loops and n^2 complexity. I want to know if there is a way to perform this action without using nested loops and less than n^2 complexity.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I recommend using reducer which would reduce your array to Map then get the entries as an array You can try this code on you chrome console

Comlexity : O(n)

var data = [{host: 'first', date: '2021-04-03', first_count: 12, second_count: 10}, {
        host: 'first',
        date: '2021-04-02',
        first_count: 10,
        second_count: 8
    }, {
        host: 'first', date: '2021-04-01', first_count: 23, second_count: 30
    }];
    mapData = data.reduce((map, currentValue) => 
            map.has(currentValue.host) ?
                map.set(currentValue.host, 
                        {host: currentValue.host, 
                        first_count_array: [...map.get(currentValue.host).first_count_array, {date: currentValue.date, first_count: currentValue.first_count}],
                        second_count_array: [...map.get(currentValue.host).second_count_array, {date: currentValue.date, second_count: currentValue.second_count}]} )
            :
                map.set(currentValue.host, {
                    host: currentValue.host,
                    first_count_array: [{date: currentValue.date, first_count: currentValue.first_count}],
                    second_count_array: [{date: currentValue.date, second_count: currentValue.second_count}]

                })
        ,
        new Map()
    );

console.log(Array.from(mapData.values()));
about 4 years ago · Juan Pablo Isaza 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