Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!