How to sum up multiple datasets [0, 1, 2, 3, 4…] with always data [0] in Javascript

👍:1

Use Array#reduce over chartdata.datasets itself.

let chartdata={labels:["Bar 1","Bar 2","Bar 3","Bar 4",],datasets:[{label:"Green data",type:"bar",data:[20,30,40,20]},{label:"Red data",type:"bar",data:[10,30,20,10]},{label:"Blue data",type:"bar",data:[20,20,20,20]},]};
let res = chartdata.datasets.reduce((acc, curr) => acc + curr.data[0], 0);
console.log(res);

Leave a comment