0

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.

cocopan
  • 109
  • 3
  • 19

2 Answers2

1

Instead of using hard-coded keys, create an array to contain the keys, and check if the object already exists in that array.

This code probably isn't exactly what you're looking for, but should give you an idea of what I'm talking about:

var products = [{
    "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"
    }
  }
]

var merged = {};
var output = document.getElementById("output")

for (var i = 0; i < products.length; i++ ) {
  Object.getOwnPropertyNames(products[i]).forEach(function(val, idx, array) {
    if (typeof(merged[val]) == "undefined") merged[val] = []
    merged[val].push(products[i][val])
  })
}

output.innerText = JSON.stringify(merged, undefined, 2)
<pre id="output">
</pre>
theGleep
  • 1,179
  • 8
  • 14
1

This took shamefully long. It's what you were looking for:

const units = [{"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"}}]

const objKey = obj => Object.keys(obj)[0]
const objValue = obj => Object.values(obj)[0]
newObj = {}
let unitName
for (let unit of units) {
  unitName = objKey(unit)
  newObj[unitName] = []
}
let unitData
for (let unit of Object.values(units)) {
  unitData = objValue(unit)
  newObj[objKey(unit)].push(unitData)
}
console.log(newObj)
Andrew
  • 7,201
  • 5
  • 25
  • 34