Search code examples
javascriptjqueryarraysjsonmorris.js

Removing duplicate Date value from a JSON string


I am working on morris line chart. The json string I am getting from the service is:

{"SDate": "2017-01-21", "A": 0, "B": 0, "C": 23},
{"SDate": "2017-01-21", "A": 10, "B": 0, "C": 0},
{"SDate": "2017-01-22", "A": 20, "B": 0, "C": 0},
{"SDate": "2017-01-23", "A": 30, "B": 0, "C": 67},
{"SDate": "2017-01-24", "A": 0, "B": 13, "C": 0},
{"SDate": "2017-01-25", "A": 70, "B": 0, "C": 0},
{"SDate": "2017-01-25", "A": 3, "B": 45, "C": 0},
{"SDate": "2017-01-26", "A": 50, "B": 0, "C": 0},
{"SDate": "2017-01-27", "A": 20, "B": 0, "C": 0},
{"SDate": "2017-01-28", "A": 80, "B": 0, "C": 0}

As you can see, the first top 2 rows have the same date with different values. I want to merge the two row as one like

{"SDate": "2017-01-21", "A": 10, "B": 0, "C": 23},

Similarly, 2017-01-25 has 2 rows,

{"SDate": "2017-01-25", "A": 70, "B": 0, "C": 0},
{"SDate": "2017-01-25", "A": 3, "B": 45, "C": 0},

Which I want to merge like

{"SDate": "2017-01-25", "A": 73, "B": 45, "C": 0},

So the final JSON string will be like

    {"SDate": "2017-01-21", "A": 10, "B": 0, "C": 23},
    {"SDate": "2017-01-22", "A": 20, "B": 0, "C": 0},
    {"SDate": "2017-01-23", "A": 30, "B": 0, "C": 67},
    {"SDate": "2017-01-24", "A": 0, "B": 13, "C": 0},
    {"SDate": "2017-01-25", "A": 73, "B": 45, "C": 0},
    {"SDate": "2017-01-26", "A": 50, "B": 0, "C": 0},
    {"SDate": "2017-01-27", "A": 20, "B": 0, "C": 0},
    {"SDate": "2017-01-28", "A": 80, "B": 0, "C": 0}

I know that ideally it should be handled in the backend, but I don't have any control over that and this needs to be fixed using javascript. Right now I am trying the solution from Sort and merge JSON keys with matching values , but not getting the desired result. Kindly help

Edit 1: Here A, B and C are not fixed. This can be different sets at all. Like X, Y or Just W or anything . eg

{"SDate": "2017-01-21", "X": 0, "Y": 0, "Z": 23},....,{"SDate": "2017-01-28", "X": 0, "Y": 0, "Z": 23}

Or

{"SDate": "2017-01-21", "W": 0, "B": 0, "V": 23},......,{"SDate": "2017-01-28", "W": 0, "B": 0, "V": 23},

and .... *The sets will be same for a specific string, but not necessarily A,B and C always


Solution

  • You can use forEach method by passing a callback function to every item from the array.

    The solution is to use a hash structure in order to retain only unique values and find out the sum of duplicate rows.

    let array=[{"SDate": "2017-01-21", "A": 0, "B": 0, "C": 23}, {"SDate": "2017-01-21", "A": 10, "B": 0, "C": 0}, {"SDate": "2017-01-22", "A": 20, "B": 0, "C": 0}, {"SDate": "2017-01-23", "A": 30, "B": 0, "C": 67}, {"SDate": "2017-01-24", "A": 0, "B": 13, "C": 0}, {"SDate": "2017-01-25", "A": 70, "B": 0, "C": 0}, {"SDate": "2017-01-25", "A": 3, "B": 45, "C": 0}, {"SDate": "2017-01-26", "A": 50, "B": 0, "C": 0}, {"SDate": "2017-01-27", "A": 20, "B": 0, "C": 0}, {"SDate": "2017-01-28", "A": 80, "B": 0, "C": 0}]
    let result=[];
    array.forEach(function (item) {
        if (!this[item.SDate]) {
            this[item.SDate] = { SDate: item.SDate };
            Object.keys(item).slice(1).forEach(function(key){
              this[item.SDate][key]=0;
            });
            result.push(this[item.SDate]);
        }
        Object.keys(item).slice(1).forEach(function(key){
              this[item.SDate][key]+=item[key];
        });
    });
    console.log(result);