Reputation: 12923
This is going to be a really question, but one I cannot figure out for the life of me. I am not even sure the correct function I could use.
Consider the following:
As you can see we have an array of objects, each object has a key (the date) and a value of an array.
As we can see there are two objects with the same date. How can I merge those two objects together such that I have one date (August 10th) with an array of two objects instead of two objects with an array of one object.
I think this would be an array method (something like filter?) with a collection method?
I am not sure. Help?
Upvotes: 0
Views: 876
Reputation: 53
//Solution starts here:
var results = [];
var temps = {};
//Iterate through the dates to find uniq keys(date).
_.each(dates, function(date) {
//Store uniq keys(date) and it's value.
_.each(date, function(value, key) {
if (temps.hasOwnProperty(key)) {
temps[key] = temps[key].concat(value);
} else {
temps[key] = value;
}
});
});
//Tranform the object into an array.
_.map(temps, function(value, key) {
var item = {};
item[key] = value;
results.push(item);
});
//results is your answer
Upvotes: 1