I'm using the following to process rows from shopping cart. I'm wanting to group the rows and sum the values in the grouping. I'm not able to get the sum to work at all and I was hoping to get some assistance please.
thanks in advance
-- data
var cart: [
{
id: 2,
title: 'Batteries',
description: 'AAA size batteries',
price: 10.99
},
{
id: 1,
title: 'Beacons',
description: 'Bluetooth long life beacons',
price: 30.00
},
{
id: 1,
title: 'Beacons',
description: 'Bluetooth long life beacons',
price: 30.00
}
]
-- code
const groupedResult = _(cart)
.groupBy('price')
.map(function(items, price, title) {
return {
title: _.map(items, 'title'),
description: _.map(items, 'description'),
price: _.sum(price, 'price'),
};
}).value()
-- current output
{title: Array(2), description: Array(2), price: "30"}
{title: Array(1), description: Array(1), price: "10.99"}
-- expected output
{title: 'Beacons', description: 'Bluetooth long life beacons', price: '30.00', total: '60.00', quantity: 2}
{title: 'Batteries', description: 'AAA size batteries', price: '10.99', total: '10.99', quantity: 1}