javascript迭代和重新映射

时间:2017-10-04 18:16:44

标签: javascript mapping

我有一个如下所示的数据集:

const data = [["customer1",29349,3654],["customer2",29349,3654],["customer3",15178,130]

我需要使用地图将数据转换为此数据。

series: [{
    category: 'Resolved before conciliation',
    data: [29349, 29349, 15178]
}, {
    category: 'Conciliation successful',
    data: [3654, 29349, 130]
}]

我尝试了下面这个让我成为我的第一个类别,但我似乎无法弄清楚如何在同一张地图中将我的第二个类别放入我的系列中。

const series = [{
    name: 'Resolved before conciliation',
    data: data.map(([...data]) => (data[1]))
}]

console.log(JSON.stringify(series))

输出:

[{"name":"Resolved before conciliation","data":[29349,29349,15178]}]

2 个答案:

答案 0 :(得分:1)

使用reduce进行分组:

re.sub

答案 1 :(得分:0)

您可以使用相同的索引将值推送到数组。

const
    data = [["customer1", 29349, 3654], ["customer2", 29349, 3654], ["customer3", 15178, 130]],
    result = { series: [{ category: 'Resolved before conciliation', data: [] }, { category: 'Conciliation successful', data: [] }] };

data.forEach(a => a.slice(1).forEach((b, i) => result.series[i].data.push(b)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }