score:0

Accepted answer

This solution groups the data by week number (using moment.js to calcualate the week number) and then maps each group so that you end with with something like:

[
   { week: 1, totalAllocated: 43.6, totalUnallocated: 23.56 },
   { week: 2, totalAllocated: 11.3, totalUnallocated: 16.68 }
   ...
]

var weekNumber = function(thingy){
    return moment(thingy.date).week();
}

var sumAllocated = function(total, thingy){
    return total + parseFloat(thingy.allocated);
}

var sumUnallocated = function(total, thingy){
    return total + parseFloat(thingy.unallocated);
}

var groups = _.groupBy(test, weekNumber);

var weeklyBreakdown = _.map(groups, function(group, week){
    return {
        week: week,
        totalAllocated : _.reduce(group, sumAllocated, 0),
        totalUnallocated : _.reduce(group, sumUnallocated, 0),
    };
});

Related Query

More Query from same tag