In my Vue application, I am faced with the challenge of grouping an array by date and then calculating sums for multiple columns. The current method I have only allows me to group and sum one column:
receiptsByDate: function(){
let byDate = _.groupBy(this.receipts, 'date');
let totals = {};
_.forEach(byDate, function(amounts, Date){
totals[Date] = _.reduce( byDate[Date], function(sum, receipt){
return sum + parseFloat( receipt.total );
}, 0);
})
return totals;
}
This function creates an object in the format of date: total.
Example of a receipt on which this function is applied:
card_commission:null
created_at:"2019-11-14 06:13:20"
customer_id:null
date:"2019-11-14"
discount:"12000.0"
id:1
location_id:null
number:"2019-00001"
service:null
subtotal:"200000.0"
table_id:null
taxes:null
ticket_id:1
total:"188000.0"
updated_at:"2019-11-14 06:13:20"
However, I now need to not only group by date but also calculate sums for other columns such as discount, subtotal, and so on. After searching online, I couldn't find a solution for this specific requirement. Can anyone provide guidance or suggest an alternative approach? It doesn't necessarily have to involve loadash; any other solution would be greatly appreciated.