我有一个可能接收重复键数据的高图,我想知道如何构建一些逻辑,将两个键/值合并为一个系列。
这是我的数据
var dataArray = [{
ErrorDate: "2017-09-07",
Brand: "Toyota",
Count: 3
}, {
ErrorDate: "2017-09-02",
Brand: "Ford",
Count: 258
}, {
ErrorDate: "2017-09-03",
Brand: "Ford",
Count: 239
}, {
ErrorDate: "2017-09-04",
Brand: "Ford",
Count: 197
}, {
ErrorDate: "2017-09-05",
Brand: "Ford",
Count: 187
}, {
ErrorDate: "2017-09-06",
Brand: "Ford",
Count: 418
}, {
ErrorDate: "2017-09-07",
Brand: "Ford",
Count: 344
}, {
ErrorDate: "2017-09-03",
Brand: "Mercedes",
Count: 43
}, {
ErrorDate: "2017-09-04",
Brand: "Mercedes",
Count: 220
}, {
ErrorDate: "2017-09-03",
Brand: "Chrysler",
Count: 3
}, {
ErrorDate: "2017-09-04",
Brand: "Chrysler",
Count: 3
}, {
ErrorDate: "2017-09-06",
Brand: "Chrysler",
Count: 6
}, {
ErrorDate: "2017-09-07",
Brand: "Chrysler",
Count: 1
}];
我必须使用'福特'和'福特'键,我想要完成的是合并这两个并在一个系列下附加值。
这是我的小提琴:https://jsfiddle.net/pvbtmx2j/
答案 0 :(得分:0)
试试这个: https://jsfiddle.net/pegla/tLd9711z/2/
var dataArray = [{
ErrorDate: "2017-09-07",
Brand: "Toyota",
Count: 3
}, {
ErrorDate: "2017-09-02",
Brand: "Ford",
Count: 258
}, {
ErrorDate: "2017-09-03",
Brand: "Ford",
Count: 239
}, {
ErrorDate: "2017-09-04",
Brand: "Ford",
Count: 197
}, {
ErrorDate: "2017-09-05",
Brand: "Ford",
Count: 187
}, {
ErrorDate: "2017-09-06",
Brand: "Ford",
Count: 418
}, {
ErrorDate: "2017-09-07",
Brand: "Ford",
Count: 344
}, {
ErrorDate: "2017-09-03",
Brand: "Mercedes",
Count: 43
}, {
ErrorDate: "2017-09-04",
Brand: "Mercedes",
Count: 220
}, {
ErrorDate: "2017-09-03",
Brand: "Chrysler",
Count: 3
}, {
ErrorDate: "2017-09-04",
Brand: "Chrysler",
Count: 3
}, {
ErrorDate: "2017-09-06",
Brand: "Chrysler",
Count: 6
}, {
ErrorDate: "2017-09-07",
Brand: "Chrysler",
Count: 1
}, {
ErrorDate: "2017-09-04",
Brand: "ford",
Count: 22
}, {
ErrorDate: "2017-09-06",
Brand: "ford",
Count: 25
}, {
ErrorDate: "2017-09-07",
Brand: "ford",
Count: 63
}].reduce((prevVal, currVal, index) => {
currVal.Brand = currVal.Brand.replace(/\b\w/g, l => l.toUpperCase());
prevVal.push(currVal);
return prevVal;
}, []);
如果您希望按日期进一步合并 - 您需要修改reduce函数,并选择更高的值或更小的值,或者将其相加。