Javascript将两个对象组合成具有唯一数据元素即年份的新对象

时间:2017-08-20 17:40:49

标签: javascript arrays

我在JS中有两个对象,其中year在两个对象中都是唯一的。我想将它们组合成一个var financials = [{"year": 2013, "rev_prod1": 10000, "rev_prod2": 5000}, {"year": 2014, "rev_prod1": 8000, "rev_prod2": 10000}, {"year": 2015, "rev_prod1": 15000, "rev_prod2": 20000}] var stats = [{"year": 2013, "units_sold": 900, "hours": 55},{"year": 2014, "units_sold": 800, "hours": 45}, {"year": 2015, "units_sold": 1000, "hours": 70}] 相同的新对象。

var combinedData = [{"year": 2013, "rev_prod1": 10000, "rev_prod2": 5000, "units_sold": 900, "hours": 55}, {"year": 2014, "rev_prod1": 8000, "rev_prod2": 10000, "units_sold": 800, "hours": 45}, {"year": 2015, "rev_prod1": 15000, "rev_prod2": 20000, "units_sold": 1000, "hours": 70}]

预期输出将如下所示:

testGraph=open(input("Enter a file name:"))
for line in testGraph:
  temp=line.split()

3 个答案:

答案 0 :(得分:1)

您可以先使用hashtable合并相似的对象,然后使用#forEach()函数提取所需的数组来创建#map() - 请参阅下面的演示:< / p>

var financials = [{"year": 2013, "rev_prod1": 10000, "rev_prod2": 5000}, {"year": 2014, "rev_prod1": 8000, "rev_prod2": 10000}, {"year": 2015, "rev_prod1": 15000, "rev_prod2": 20000}];
var stats = [{"year": 2013, "units_sold": 900, "hours": 55},{"year": 2014, "units_sold": 800, "hours": 45}, {"year": 2015, "units_sold": 1000, "hours": 70}]
var hash = {};

// function to create a hashtable
function classify(e) {
   if(hash[e.year]) {
     Object.keys(e).forEach(function(c){
        hash[e.year][c] = e[c];
     });
   } else {
     hash[e.year] = e;
   }
}

// add to hash
financials.forEach(classify);
stats.forEach(classify);

// extract the result
var result = Object.keys(hash).map(function(e){
  return hash[e];
});

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

答案 1 :(得分:1)

您可以为stats对象使用哈希表,并通过迭代键来合并值。

&#13;
&#13;
var financials = [{ "year": 2013, "rev_prod1": 10000, "rev_prod2": 5000 }, { "year": 2014, "rev_prod1": 8000, "rev_prod2": 10000 }, { "year": 2015, "rev_prod1": 15000, "rev_prod2": 20000 }],
    stats = [{ "year": 2013, "units_sold": 900, "hours": 55 }, { "year": 2014, "units_sold": 800, "hours": 45 }, { "year": 2015, "units_sold": 1000, "hours": 70 }],
    hash = Object.create(null);

stats.forEach(function (o) {
    hash[o.year] = o;
});

financials.forEach(function (o) {
    Object.keys(hash[o.year]).forEach(function (k) {
        o[k] = hash[o.year][k];
    });
});

console.log(financials);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

ES6与MapObject.assign

&#13;
&#13;
var financials = [{ "year": 2013, "rev_prod1": 10000, "rev_prod2": 5000 }, { "year": 2014, "rev_prod1": 8000, "rev_prod2": 10000 }, { "year": 2015, "rev_prod1": 15000, "rev_prod2": 20000 }],
    stats = [{ "year": 2013, "units_sold": 900, "hours": 55 }, { "year": 2014, "units_sold": 800, "hours": 45 }, { "year": 2015, "units_sold": 1000, "hours": 70 }],
    map = new Map(stats.map(o => [o.year, o]));

financials.forEach(o => Object.assign(o, map.get(o.year)));

console.log(financials);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我写了一个函数来将2个列表与公共属性分组,这是你想要的吗?

indexOf