从数组中获取数据并将其排列到JS数组的不同结构中

Get the data from a array and arrange it to different structure of JS array

我有一个数组,我需要从该数组中获取数据并排列一个具有不同结构的新数组。

我有的排列

[[21,1],[22,2],[32,4],[35,6],[45,1],[82,1]]

排列我所需要的

[[0-10,0],[10-20,0],[20-30,3],[30-40,10],[40-50,1],[80-90,1] ]

我们可以使用 JavaScript 来实现吗?

您可以将范围值除以十并加上计数值

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; }