so i have an array of object like this:
const data =[{
"username": "MT220047",
"title": "Test",
"position": "Management Trainee",
"department": "Logistic",
"2022-11-15": 1,
"2022-11-16": 2
},
{
"username": "MT220047",
"title": "UntitledForm1",
"position": "Management Trainee",
"department": "Logistic",
"2022-11-16": 1
},
{
"username": "17000691",
"title": "Test",
"position": "Foreman",
"department": "Production",
"2022-11-16": 1,
}
]
const date =["2022-11-11","2022-11-12","2022-11-13","2022-11-14","2022-11-15","2022-11-16"]
const accumulator = {}
const final =[]
const res = date.map((tanggal)=>{ return data.reduce((a,e)=>(a[e.tanggal]={...(a[e.tanggal] || {}), ...e}, a),{});})
console.log(res)
it try to get the summary or pivot value of the dates, but i get this result instead of what i expected:
[ { undefined:
{ username: '17000691',
title: 'Test',
position: 'Foreman',
department: 'Production',
'2022-11-15': 1,
'2022-11-16': 1 } },
{ undefined:
{ username: '17000691',
title: 'Test',
position: 'Foreman',
department: 'Production',
'2022-11-15': 1,
'2022-11-16': 1 } },
{ undefined:
{ username: '17000691',
title: 'Test',
position: 'Foreman',
department: 'Production',
'2022-11-15': 1,
'2022-11-16': 1 } },
....]
my expected result should be like this: i try to get the summary total of the dates base on the date array...
summary_date:[ {"2022-11-15": 1},{"2022-11-16": 4}]
or is it possible to get this result:
data = [{"date":"2022-11-16","total":4},{"date":"2022-11-15","total":1}]
any help on this, or can someone tellme where did i do wrong here?