2017-09-29 7 views
0

ich auf morris Liniendiagramm arbeite. Der JSON-String Ich bin aus dem Dienst bekommen ist:doppelte Date-Wert aus einem String JSON Entfernen

{"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} 

Wie Sie sehen können, die ersten Top-2 Reihen mit dem gleichen Datum mit unterschiedlichen Werten haben. Ich möchte in ähnlicher Weise die beiden Reihe als eine wie

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

fusionieren, 2017-01-25 hat zwei Reihen,

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

Was ich wie

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

Also die letzte JSON-String zusammenführen möchten wird wie

sein
{"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} 

Ich weiß, dass im Idealfall sollte es im Backend behandelt werden, aber ich habe keine Kontrolle über die und das repariert werden muss Javascript. Im Moment versuche ich, die Lösung von Sort and merge JSON keys with matching values, aber nicht das gewünschte Ergebnis zu bekommen. Hier A, B und C nicht festgelegt sind: freundlicher

Edit 1 helfen. Dies können verschiedene Sätze überhaupt sein. Wie X, Y oder Just W oder irgendetwas. zB

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

Oder

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

und .... * Die Sätze für eine bestimmte Zeichenfolge seines gleichen, aber nicht notwendigerweise A, B und C immer

+0

Also, was passiert, wenn mehrere Zeilen haben einen Wert für ' "A"'? – Cerbrus

+0

Ist das wirklich die vollständige Zeichenfolge, die Sie von der Antwort erhalten? Hat es kein enthaltendes Element? – palasjir

+0

@ Cerbrus Es wird den ganzen Wert hinzufügen, wie in meinem 2. Fall @palasjir Ja, das ist die Zeichenfolge, die ich bekomme –

Antwort

2

können Sie verwenden forEach Verfahren durch eine Callback- Funktion jedes Element aus dem Array vorbei.

Die Lösung besteht darin, eine hash Struktur zu verwenden, um nur eindeutige Werte beizubehalten und die Summe der doppelten Zeilen herauszufinden.

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

+0

Danke Kamerad. Funktioniert jetzt gut. Nur aus Neugier, ich arbeitete mit Ihrem Code für 2 Sätze von Arrays, aber es bricht dazwischen. Können Sie das überprüfen https://jsfiddle.net/nishantaujju/Lwwz4fga/2/ –

1

Sie können es in der folgenden Art und Weise tun

let arr = [ 
 
{"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} 
 
]; 
 

 
arr = arr.sort((a, b) => { 
 
     if(a.SDate < b.SDate) 
 
      return -1; 
 
     if(a.SDate > b.SDate) 
 
      return 1; 
 
     return 0; 
 
}).reduce((a, b) => { 
 
    if(a.length == 0){ 
 
     a.push(b); 
 
    } 
 
    else if(a[a.length-1].SDate != b.SDate){ 
 
     a.push(b); 
 
    } 
 
    else { 
 
     a[a.length-1].A += b.A; 
 
     a[a.length-1].B += b.B; 
 
     a[a.length-1].C += b.C; 
 
    } 
 
    return a; 
 
}, []) 
 

 
console.log(arr);

+0

Dies funktioniert nicht. Versuchen Sie es mit der doppelten Anzahl von Einträgen im Array. – Cerbrus

+0

aktualisiert den Fehler in der Sortierung – marvel308

+0

@ Cerbrus https://ideone.com/PysGGA – marvel308

1

Hier können Sie einen anderen Ansatz haben ...

var yourData = [ 
    {"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} 
]; 

var newData = {}; 

// Create an object using dates as indexes, so we can find it on each loop. 
$.each(yourData,function(id,value) { 

    if (!newData[value['SDate']]) 
     newData[value['SDate']] = {"SDate": value.SDate, "A": 0, "B": 0, "C": 0}; 

    newData[value['SDate']]["A"] += value.A; 
    newData[value['SDate']]["B"] += value.B; 
    newData[value['SDate']]["C"] += value.C; 
}); 

// Remove indexes and get the desired array of values. 
newData = $.map(newData,function(value,index) { 
    return value; 
}); 

console.log(newData); 

Ich hoffe, es hilft

1

Sie könnten einen dynamischen Ansatz mit einer Hash-Tabelle und eine Variable für den Schlüssel, den Sie gerne gruppieren.

Der Rest aller Eigenschaften sind zum Summieren genommen.

var data = [{ 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 }], 
 
    groupBy = 'SDate', 
 
    hash = Object.create(null), 
 
    grouped = []; 
 

 
data.forEach(function (o) { 
 
    if (!hash[o[groupBy]]) { 
 
     hash[o[groupBy]] = {}; 
 
     hash[o[groupBy]][groupBy] = o[groupBy]; 
 
     grouped.push(hash[o[groupBy]]); 
 
    } 
 
    Object.keys(o).forEach(function (k) { 
 
     if (k === groupBy) { 
 
      return; 
 
     } 
 
     hash[o[groupBy]][k] = (hash[o[groupBy]][k] || 0) + o[k]; 
 
    }); 
 
}); 
 
    
 
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Verwandte Themen