2017-11-01 2 views
-1

Ich habe ein Array von Objekten wie folgt und ich möchte die Menge der Objekte hinzufügen, deren Product_id gleich ist.Schleife durch Array von Objekten und Objekte hinzufügen, die die gleiche ID haben

[ 
     { 
     "BARKOD": "Pa Detatime", 
     "DETAJIM1": "", 
     "DETAJIM2": "", 
     "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", 
     "product_id": "SD13137", 
     "KODI": "MX02", 
     "KODNJESIA1": "cope", 
     "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", 
     "cmimibaze": 0, 
     "quantity": 1 
     }, 
     { 
     "BARKOD": "Pa Detatime", 
     "DETAJIM1": "", 
     "DETAJIM2": "", 
     "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", 
     "product_id": "SD13137", 
     "KODI": "MX03", 
     "KODNJESIA1": "cope", 
     "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", 
     "cmimibaze": 0, 
     "quantity": 3 
     }, 
     { 
     "BARKOD": "Pa Detatime", 
     "DETAJIM1": "", 
     "DETAJIM2": "", 
     "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", 
     "product_id": "SD13141", 
     "KODI": "MX02", 
     "KODNJESIA1": "cope", 
     "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", 
     "cmimibaze": 0, 
     "quantity": 1 
     } 
    ] 

Also das Ende Array sollte wie unten sein:

[ 
     { 
     "BARKOD": "Pa Detatime", 
     "DETAJIM1": "", 
     "DETAJIM2": "", 
     "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", 
     "product_id": "SD13137", 
     "KODI": "MX02", 
     "KODNJESIA1": "cope", 
     "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", 
     "cmimibaze": 0, 
     "quantity": 4 
     }, 
     { 
     "BARKOD": "Pa Detatime", 
     "DETAJIM1": "", 
     "DETAJIM2": "", 
     "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", 
     "product_id": "SD13141", 
     "KODI": "MX02", 
     "KODNJESIA1": "cope", 
     "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", 
     "cmimibaze": 0, 
     "quantity": 1 
     } 
    ] 

ich diese Frage gestellt zusammen mit einem anderen here konnte aber nur die andere Frage lösen. Wenn jemand lösen könnte, wäre ich sehr dankbar. Danke.

+0

Aber die "KODI" Eigenschaft im Objekt 1 und Objekt 2 MX02 und Mx03 jeweils . Möchtest du die beiden Objekte wirklich zusammenführen? – darthaditya

+0

@darthaditya Ja, dieser Teil ist nicht wichtig, Sie können es als MX02 oder MX03 lassen, spielt keine Rolle. – Alfred

+0

Bitte nimm die [Tour], schau dich um und lies die [Hilfe], insbesondere [* Wie stelle ich eine gute Frage? *] (/ Help/how-to-ask) Recherchiere. [Suche] (/ help/searching) für verwandte Themen zu SO. Versuch, die Arbeit zu machen. ** Wenn ** du feststeckst und dich nicht mehr lösen kannst, nachdem du mehr geforscht und gesucht hast, poste einen [mcve] von deinem Versuch und sage genau, wo du feststeckst. –

Antwort

1

könnten Sie reduzieren, wie folgt aus:

let input = [{"BARKOD": "Pa Detatime","DETAJIM1": "","ETAJIM2": "","DTMODIFIKIM": "2017-10-02T16:06:53.206Z","product_id": "SD13137","KODI": "MX02","KODNJESIA1": "cope","PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56","cmimibaze": 0,"quantity": 1},{"BARKOD": "Pa Detatime","DETAJIM1": "","DETAJIM2": "","DTMODIFIKIM": "2017-10-02T16:06:53.206Z","product_id": "SD13137","KODI": "MX03","KODNJESIA1": "cope","PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56","cmimibaze": 0,"quantity": 3},{"BARKOD": "Pa Detatime","DETAJIM1": "","DETAJIM2": "","DTMODIFIKIM": "2017-10-02T16:06:53.206Z","product_id": "SD13141","KODI": "MX02","KODNJESIA1": "cope","PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56","cmimibaze": 0,"quantity": 1}]; 
 

 
let output = input.reduce(function(res, el) { 
 
    if(res[el.product_id]) { 
 
    res[el.product_id].quantity += el.quantity; 
 
    } else { 
 
    res[el.product_id] = el; 
 
    } 
 
    return res; 
 
}, {}); 
 

 
let outputArr = Object.values(output); 
 

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

0

var data = [{"BARKOD": "Pa Detatime", "DETAJIM1": "", "DETAJIM2": "", "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", "product_id": "SD13137", "KODI": "MX02", "KODNJESIA1": "cope", "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", "cmimibaze": 0, "quantity": 1 }, {"BARKOD": "Pa Detatime", "DETAJIM1": "", "DETAJIM2": "", "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", "product_id": "SD13137", "KODI": "MX03", "KODNJESIA1": "cope", "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", "cmimibaze": 0, "quantity": 3 }, {"BARKOD": "Pa Detatime", "DETAJIM1": "", "DETAJIM2": "", "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", "product_id": "SD13141", "KODI": "MX02", "KODNJESIA1": "cope", "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", "cmimibaze": 0, "quantity": 1 } ] 
 
var mapObj = {}; 
 
for(var a of data){ 
 
    if(mapObj[a["product_id"]]== undefined) 
 
     mapObj[a["product_id"]] = 0; 
 
    mapObj[a["product_id"]] += a["quantity"] 
 
} 
 
var data2 = []; 
 
for(var a of data){ 
 
    if(mapObj[a["product_id"]] == undefined) 
 
    continue; 
 
    a["quantity"] = mapObj[a["product_id"]]; 
 
    data2.push(a) 
 
    delete mapObj[a["product_id"]]; 
 
} 
 
console.log(data2)

Verwandte Themen