I have a array and I need to get the data from that array and arrange a new array with different structure.
Array what I have
[[21,1],[22,2],[32,4],[35,6],[45,1],[82,1]]
Array what I need
[[0-10,0],[10-20,0],[20-30,3],[30-40,10],[40-50,1],[80-90,1] ]
can we do it using JavaScript?
You could divide the range value by ten and add the count value
const
data = [[21, 1], [22, 2], [32, 4], [35, 6], [45, 1], [82, 1]],
result = data.reduce((r, [v, c]) => {
const i = Math.floor(v / 10);
while (r.length <= i) r.push([`${r.length * 10}-${(r.length + 1) * 10 - 1}`, 0]);
r[i][1] += c;
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }