Reputation: 1191
I have following two objects in JS where year
is unique in both objects. I want to combine them into a new object where the year
is same.
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}]
The expected output will look like this:
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}]
Upvotes: 2
Views: 62
Reputation:
I've wrote a function to grouping 2 list with common property, is that what you want?
function commonProps(list1, list2, common) {
var output = [], commons = {}, combined = list1.concat(list2)
for(var i in combined){
var item = combined[i], $index
if(commons[item[common]] != null){
$index = commons[item[common]]
for(var j in item){
output[$index][j] = item[j]
}
} else {
commons[item[common]] = output.push(item) -1
}
}
return output
}
Upvotes: 1
Reputation: 386746
You could use a hash table for the stats
objects and merge the values by iterating the keys.
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);
.as-console-wrapper { max-height: 100% !important; top: 0; }
ES6 with Map
and Object.assign
.
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);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1
Reputation: 42370
You can first create a hashtable
by merging similar objects using #forEach()
and then extract the required array using a #map()
function - see demo below:
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;}
Upvotes: 1