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:

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