I have an array of items which have a date field. I need to group items by dates so that on my page I can render them grouped by this date. Something like this:
Monday 1st:
-event 1
-event 2
Tuesday 2nd:
-event 3
This works but I'm doing quite a lot of looping. Is there a more performant solution? Or is my approach fine? Ive simplified my code but in reality I will have a much larger dataset.
const items=[{name: '1',date: '10 dec'},{name: '2',date: '10 dec'},{name: '3',date: '11 dec' }];
const dates = items.map(item=> item.date)
const datesNoDupe = Array.from(new Set(dates));
const output = datesNoDupe.map(date=>{
return items.filter(item=>{
return item.date === date;
});
})
console.log(output)