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

113
Views
Transform data into nested JSON

I am working with javascript for the first time, and I have an array of around 35000 objects, where each object looks like this: {city: 'city1', buildingtype: 'buildingtype1', day: 1, hour: 1, energy: 59}

I want to sort this data into a "nested" JSON object, with cities as the first object. For each city, I then need to sort by buildingtype, and for each buildingtype I want an array with the energy, sorted by time.

I have added an image here which might explain it a bit better: Image of what I want to achieve

I have begun with something like this

  let transformed_data = {}


  $:{
    datapoints.forEach((d)=> {
      if(!(d.city in transformed_data)){
        transformed_data[city] = {};
      }

      }
    )
  }

Would this be a good way to move forward?

Edit: Thanks to some nice help this is the solution I found, where I used that the data was already sorted to my advantage.

  let transformed_data = {}


  $:{
    datapoints.forEach((d)=> {
      if(!(d.city in transformed_data)){
        transformed_data[d.city] = {};
      }
      if (!(d.buildingtype in transformed_data[d.city])){
        transformed_data[d.city][d.buildingtype] = [];
      }
      transformed_data[d.city][d.buildingtype].push(d.total_heating_energy)
      }
    )
  } 

The resulting object can be found here: Final_result

about 4 years ago · Juan Pablo Isaza
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!