如何在不改变对象内部对象的情况下合并数组中的多维对象

时间:2017-10-13 16:48:52

标签: javascript arrays object merge

您好:)我需要以下方面的帮助:

我有这个:

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

我希望得到这样的结果:

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

我一直试图找到一种方法来做到这一点,但遗憾的是我还没有能力。到目前为止,我只能做一些非常不灵活的事情:

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"])
         }
       }
     )
 }

这种方式有用......它给了我一些工作但是我有多个&#34; vals&#34;因此,如果我添加一个新的,我将不得不每次都编辑我的代码以添加该异常...我想看看是否有办法更有效地做到这一点...我也理解我的当前方法并没有完全给出我想要的东西,但这种格式允许我用数据做我需要的东西。对不起,如果这是太基本的x_x

我尝试了这个Merge duplicate multidimensional objects in array of objects,但这种方法合并了我的所有对象而没有考虑日期,我关心日期。

2 个答案:

答案 0 :(得分:1)

不使用硬编码键,而是创建一个包含键的数组,并检查该对象是否已存在于该数组中。

此代码可能并非您正在寻找的内容,但应该让您知道我在谈论的内容:

&#13;
&#13;
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)
&#13;
<pre id="output">
</pre>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这花了很长时间。这就是你要找的东西:

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)