Reputation: 223
I have a highchart that potentially receives data with duplicate keys, I was wondering how I can build some logic that'll merge the two key/values together into one series.
This is my data
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
}];
I have to keys with 'Ford' and 'ford' and what I want to accomplish is to merge those two and append there values under one series.
Here is my fiddle: https://jsfiddle.net/pvbtmx2j/
Upvotes: 0
Views: 44
Reputation: 1866
Try this: 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;
}, []);
If you want to merge it further by date - you will need to modify reduce function, and chose higher value or smaller value or add one to another.
Upvotes: 0