Seeking help with converting an array to a specific data format for chart display.
The chrart.js library requires data in the following format:
dataset = [ { label: 'one', data: []},
{label: 'two', data: []}
];
I receive response data in another format, which is in random order and needs to be appropriately changed with the respective labels.
Here is the code and trial I have attempted:
const dataset = [
{
detail: {
team: [
{ name: 'alpha', game: 1 },
{ name: 'beta', game: 1 },
{ name: 'gamma', game: 1 },
{ name: 'delta', game: 1 },
{ name: 'echo', game: 1 }
]
}
},
// more data objects here...
];
const teams = dataset.map(ds => ds.detail.team);
let z = teams.map(element => {
return element.map(e => {
let p = {};
let n = e.name;
let c = e.game;
p[n] = c;
return p;
});
});
console.log('z', z);
let nt = [];
z.reduce((c, n, i, a) => {
let z1 = n.map(i => {
console.log(i);
let entries = Object.entries(i);
return entries.map(e => {
return { label: e[0], data: e[1] };
});
});
return z1;
}, [])
Desired output:
[
{
label: 'alpha',
data: [1, 2, 0, 0, 2, 0]
},
// more formatted data objects...
]
I am struggling with the array.reduce
method to achieve the desired output. Any ES6 solution suggestions would be greatly appreciated.
If you can provide any insights or assistance, it would be highly valued. Thank you!