so i have
const att = [
{
date: '2022-11-04',
attendance: [{ playerName: 'Play Last', status: 'present' }],
},
{
date: '2022-11-04',
attendance: [{ playerName: 'New Player Jona', status: 'absent' }],
},
{
date: '2022-11-04',
attendance: [{ playerName: 'Jona Hysi', status: 'present' }],
},
{
date: '2022-11-04',
attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }],
},
{
date: '2022-12-04',
attendance: [{ playerName: 'Play Last', status: 'present' }],
},
{
date: '2022-12-04',
attendance: [{ playerName: 'New Player Jona', status: 'sick' }],
},
{
date: '2022-12-04',
attendance: [{ playerName: 'Jona Hysi', status: 'present' }],
},
{
date: '2022-12-04',
attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }],
},
{
date: '2022-04-14',
attendance: [{ playerName: 'Play Last', status: 'present' }],
},
{
date: '2022-04-14',
attendance: [{ playerName: 'New Player Jona', status: 'present' }],
},
{
date: '2022-04-14',
attendance: [{ playerName: 'Jona Hysi', status: 'present' }],
},
{
date: '2022-04-14',
attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }],
},
];
and i want to turn it into
const final = [
{
date: '2022-11-04',
attendance: [
{ playerName: 'Play Last', status: 'present' },
{ playerName: 'New Player Jona', status: 'absent' },
{ playerName: 'Jona Hysi', status: 'present' },
{ playerName: 'Jurgen Tafaj', status: 'present' },
],
},
{
date: '2022-12-04',
attendance: [
{ playerName: 'Play Last', status: 'present' },
{ playerName: 'New Player Jona', status: 'sick' },
{ playerName: 'Jona Hysi', status: 'present' },
{ playerName: 'Jurgen Tafaj', status: 'present' },
],
},
{
date: '2022-04-14',
attendance: [
{ playerName: 'Play Last', status: 'present' },
{ playerName: 'New Player Jona', status: 'present' },
{ playerName: 'Jona Hysi', status: 'present' },
{ playerName: 'Jurgen Tafaj', status: 'present' },
],
},
];
so i want to push the object in an array when they have the same date and end up with the date and all their attendance for that date
i tried: Merge objects in an array if they have the same date but that wasnt with an array with the object and when they have the same name (i have attendance for every object and the example had different names)
i set up a workplace here : https://stackblitz.com/edit/react-vu27ry
>Solution :
Using Array#reduce, iterate over the array while updating a Map where the key is the date and the value is the attendance array. In the end, the map values will be the list of grouped objects
const arr = [
{
date: '2022-11-04',
attendance: [{ playerName: 'Play Last', status: 'present' }]
},
{
date: '2022-11-04',
attendance: [{ playerName: 'New Player Jona', status: 'absent' }]
},
{
date: '2022-11-04',
attendance: [{ playerName: 'Jona Hysi', status: 'present' }]
},
{
date: '2022-11-04',
attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }]
},
{
date: '2022-12-04',
attendance: [{ playerName: 'Play Last', status: 'present' }]
},
{
date: '2022-12-04',
attendance: [{ playerName: 'New Player Jona', status: 'sick' }]
},
{
date: '2022-12-04',
attendance: [{ playerName: 'Jona Hysi', status: 'present' }]
},
{
date: '2022-12-04',
attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }]
},
{
date: '2022-04-14',
attendance: [{ playerName: 'Play Last', status: 'present' }]
},
{
date: '2022-04-14',
attendance: [{ playerName: 'New Player Jona', status: 'present' }]
},
{
date: '2022-04-14',
attendance: [{ playerName: 'Jona Hysi', status: 'present' }]
},
{
date: '2022-04-14',
attendance: [{ playerName: 'Jurgen Tafaj', status: 'present' }]
}
];
const res = [...
arr.reduce((map, { date, attendance = [] }) => {
const { attendance: dateAttendance = [] } = map.get(date) ?? {};
map.set(date, { date, attendance: [...dateAttendance, ...attendance] });
return map;
}, new Map)
.values()
];
console.log(res);