{
"results": [
{
id: 1,
data: [
{
name: 'Egor',
value: 10
},
{
name: 'Ivan',
value: 1
},
]
},
{
id: 1,
data: [
{
name: 'Egor',
value: 2
}
]
}
}
['Egor', 'Ivan']
), а второе - сумма value для каждого labels ([12, 1]
):personInfo = {
labels: ['Egor', 'Ivan'],
values: [12, 1]
}
const computed = results.reduce(
(acc, item) => (item.data.forEach(({ name, value }) => (acc[name] = (acc[name] ?? 0) + value)), acc),
{}
);
const personInfo = {
labels: Object.keys(computed),
values: Object.values(computed)
};