Here is the data I have:
var dates = [
{date: "2000-01-01", total: 120},
{date: "2000-10-10", total: 100},
{date: "2010-02-08", total: 100},
{date: "2010-02-09", total: 300}
];
My goal is to group and sum the totals by year like this.
var grouped = [
{date: "2000", total: 220},
{date: "2010", total: 100}
];
I tried using underscorejs but couldn't find a suitable function.
var byYear = _.map(dates, function(item){
var year = new Date(Date.parse(item.date)).getFullYear();
return {date: year, total: item.total};
});
You can see the working code here.