Hello :) I need some help with the following:
I have this:
[{"Condenser /Heat Pump":{"2":299,"starttime":"10/05/17","productClass":"Condenser /Heat Pump"}},
{"Condenser /Heat Pump":{"1":2529,"2":2270,"starttime":"10/06/17","productClass":"Condenser /Heat Pump"}},
{"Coorporate Coils":{"2":104,"3":216,"starttime":"10/05/17","productClass":"Coorporate Coils"}},
{"Coorporate Coils":{"1":1107,"2":811,"3":0,"starttime":"10/06/17","productClass":"Coorporate Coils"}}]
and i would like to get it like this:
[{"Condenser /Heat Pump":{"2":299,"starttime":"10/05/17","productClass":"Condenser /Heat Pump"},{"1":2529,"2":2270,"starttime":"10/06/17","productClass":"Condenser /Heat Pump"}},{"Coorporate Coils":{"2":104,"3":216,"starttime":"10/05/17","productClass":"Coorporate Coils"},{"1":1107,"2":811,"3":0,"starttime":"10/06/17","productClass":"Coorporate Coils"}}]
I've been trying to find a way to do this but unfortunately I havent been able. So far I have only been able to do something very inflexible:
var hp=[];
var cc=[];
for(i=0; i<mergedProductClasses.length; i++){
Object.getOwnPropertyNames(mergedProductClasses[i]).forEach(
function (val, idx, array) {
if(val =="Condenser /Heat Pump"){
hp.push(mergedProductClasses[i]["Condenser /Heat Pump"])
}else if(val =="Coorporate Coils"){
cc.push(mergedProductClasses[i]["Coorporate Coils"])
}
}
)
}
This kinda works... it gives me something to work with BUT I have multiple "vals" so if I ever add a new one I will have to be editing my code every single time to add that exception ... I would like to see if there is a way to do this more efficiently... Also I understand that my current approach doesn't give me EXACTLY what I want but this format allows me to do what I need with the data. Sorry if this is too basic x_x
I tried this Merge duplicate multidimensional objects in array of objects but this approach merges all my objects without considering the dates and I care about the dates.