Hello I have data given in that kind of form:
const mockData = [
{
id: 1,
title: 'Lorem ipsum dolor',
assignee: 'Mr First',
teams: 'team first',
dueDate: 'Monday',
status: 'CLOSE',
},
{
id: 2,
title: 'Sit amet',
assignee: 'Mr second',
teams: 'teams second',
dueDate: '10/10/2022',
status: 'OPEN',
},
]
and I need to aggregate them to this kind of shape:
const aggregated = [
{
id: 1,
title: 'title',
items: [
'Lorem ipsum dolor',
'Sit amet',
],
},
{
id: 2,
title: 'assignee',
items: [
'Mr second',
'Mr second',
],
},
{
id: 3,
title: 'teams',
items: [
'team first',
'team second',
],
},
...
]
But I'm not really sure how to achieve that. Here is my code however there is nothing really to show as it is totally wrong approach I assume because it generates plenty of repeated elements
const output = mockData.reduce((acc: AgreggatedType, item: ItemType) => {
Object.keys(item)
.forEach((key) => {
if (key) {
acc.push({
id: item.id,
title: key,
items: [],
});
}
});
return acc;
}, []);