У меня есть следующие данные:
// Data
const groups = [
[
{ group: 'All', entries: 121, score: 10.89, grade: 16, avg: 15.8 },
{ group: 'All', entries: 221, score: 20.89, grade: 26, avg: 25.8 },
{ group: 'All', entries: 321, score: 30.89, grade: 36, avg: 35.8 },
{ group: 'All', entries: 421, score: 40.89, grade: 46, avg: 45.8 }
],
[
{ group: 'Inner', entries: 121, score: 10.89, grade: 16, avg: 15.8 },
{ group: 'Inner', entries: 221, score: 20.89, grade: 26, avg: 25.8 },
{ group: 'Inner', entries: 321, score: 30.89, grade: 36, avg: 35.8 },
{ group: 'Inner', entries: 421, score: 40.89, grade: 46, avg: 45.8 }
],
[
{ group: 'Outer', entries: 121, score: 10.89, grade: 16, avg: 15.8 },
{ group: 'Outer', entries: 221, score: 20.89, grade: 26, avg: 25.8 },
{ group: 'Outer', entries: 321, score: 30.89, grade: 36, avg: 35.8 },
{ group: 'Outer', entries: 421, score: 40.89, grade: 46, avg: 45.8 }
]
];
Мой желаемый результат:
// Desired
[
[
{
group: 'All',
entries0: 121,
score0: 10.89,
grade0: 16,
avg0: 15.8,
entries1: 121,
score2: 10.89,
grade2: 16,
avg2: 15.8,
entries3: 121,
score3: 10.89,
grade3: 16,
avg3: 15.8,
entries4: 121,
score4: 10.89,
grade4: 16,
avg4: 15.8
}
]
... all other groups formatted the same
];
Пожалуйста, кто-нибудь может указать мне правильное направление? Я пробовал множество вариантов, но мой код пока выглядит следующим образом:
groups.map((group) => {
return Object.values(group).reduce((acc, curr) => {
!acc.includes(curr.group) &&
acc.push({
group: curr.group,
...curr.score
});
return acc;
});
});