Я пытаюсь суммировать с groupBy и sumBy с lodash на коде vuejs. Я тоже попробую эту ссылку
https://codepen.io/cmtliton/pen/bjNNNq?editors=1010
new Vue({
el: '#app',
data () {
return {
SaleReports : [
{
ProductId: "PVk2WIKjZJ",
quantity: 2,
MRP: 15
},
{
ProductId: "PQ_bCOJx5h",
quantity: 2,
MRP: 250
},
{
ProductId: "PVk2WIKjZJ",
quantity: 1,
MRP: 15
},
{
ProductId: "PQ_bCOJx5h",
quantity: 1,
MRP: 250
}
],
SummationReports : []
}
},
computed: {
getSummationSalesReport () {
return _(this.SaleReports)
.groupBy('ProductId')
.map((g, ProductId) => {
return {
ProductId: ProductId,
quantity: _.sumBy(g, 'quantity'),
MRP: _.sumBy(g, 'MRP' * 'quantity')
}
})
.values()
.value()
}
},
methods: {
},
created () {
this.SummationReports = this.getSummationSalesReport
}
})
{{$data | json}}
Мне нужен результат, как показано ниже:
"SummationReports": [
{
"ProductId": "PVk2WIKjZJ",
"quantity": 3,
"MRP": 750
},
{
"ProductId": "PQ_bCOJx5h",
"quantity": 3,
"MRP": 45
}
]