I am working with the following dataset:
const data = [
{
"employee_name": "Employee A",
"commission_date": "14/05/2018",
"commission_price": 9000
},
{
"employee_name": "Employee A",
"commission_date": "17/05/2018",
"commission_price": 5000
},
{
"employee_name": "Employee B",
"commission_date": "18/05/2018",
"commission_price": 2000
},
{
"employee_name": "Employee B",
"commission_date": "18/05/2018",
"commission_price": 5000
},
{
"employee_name": "Employee B",
"commission_date": "19/05/2018",
"commission_price": 500
}
]
I need to calculate the total commission_price
based on matching employee_name
and commission_date
.
The expected output should be:
[
{
"employee_name": "Employee A",
"commission_date": "14/05/2018",
"commission_price": 9000
},
{
"employee_name": "Employee A",
"commission_date": "17/05/2018",
"commission_price": 5000
},
{
"employee_name": "Employee B",
"commission_date": "18/05/2018",
"commission_price": 7000
},
{
"employee_name": "Employee B",
"commission_date": "19/05/2018",
"commission_price": 500
}
]
I have attempted to implement a solution using code from similar questions, but the results are only grouped by employee_name
. Here is the code snippet I used:
let result = Object.values(data.reduce((r, { employee_name, commission_price }) => {
r[employee_name] = r[employee_name] || { employee_name, commission_price : 0 }
r[employee_name].commission_price += commission_price
return r
}, {}))
Thank you.