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

117
Visualizações
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
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