2017-12-08 8 views
0

Ich wollte Funktionen höherer Ordnung wie Map, Filter, Reduce verwenden.
Mein Problem ist, ich habe ein Array, das nicht organisiert ist, also musste ich eine Schleife durchführen, um das gewünschte Ergebnis zu erhalten. Ein Array hat zwei Felder eins ist _id und totalCount Das ist ein Array die Länge wird mindestens 0 bis 3.
Die totalCount jede Iteration besteht aus zwei Feldern ist ein Orderstatus und ein anderes ist insgesamt Ich möchte eine normale wenn sonst Bedingung wenn ich orderstatus ist "Merchant" oder "driver" oder "user" Ich möchte diese iterierte Summe nehmen und in meinem Array wie MerchantCount speichern.Verwenden von Funktionen höherer Ordnung anstelle von zwei Schleifen

Dies ist mein Code:

var arr = [{ 
    _id: "2017-12-08", 
    totalcount: [{ 
    orderstatus: "MERCHANT", 
    total: 1 
    }] 
}, 
{ 
    _id: "2017-12-02", 
    totalcount: [{ 
    orderstatus: "USER", 
    total: 2 
    }] 
}, 
{ 
    _id: "2017-12-06", 
    totalcount: [{ 
    orderstatus: "DRIVER", 
    total: 1 
    }] 
}, 
{ 
    _id: "2017-12-07", 
    totalcount: [{ 
    orderstatus: "MERCHANT", 
    total: 3 
    }] 
}, 
{ 
    _id: "2017-12-04", 
    totalcount: [{ 
     orderstatus: "DRIVER", 
     total: 1 
    }, 
    { 
     orderstatus: "MERCHANT", 
     total: 2 
    }, 
    { 
     orderstatus: "USER", 
     total: 1 
    } 
    ] 
} 
] 

Die Schleife Durchführung am:

for (let i = 0; i < recentUserCount.length; i++) { 
    for (let j = 0; j < recentUserCount[i].totalcount.length; j++) { 
    if (recentUserCount[i].totalcount[j].usertype == "DRIVER") { 
     recentUserCount[i].DRIVERCount = recentUserCount[i].totalcount[j].total; 
    } else if (recentUserCount[i].totalcount[j].usertype == "USER") { 
     recentUserCount[i].USERCount = recentUserCount[i].totalcount[j].total; 
    } else if (recentUserCount[i].totalcount[j].usertype == "MERCHANT") { 
     recentUserCount[i].MERCHANTCount = recentUserCount[i].totalcount[j].total; 
    } 
    } 
} 

Das Ergebnis etwa wie unten sein wird:

0 : {_id: "2017-12-08", totalcount: Array(1), MERCHANTCount: 3, $$hashKey: "object:316"} 
    1 : {_id: "2017-12-07", totalcount: Array(1), MERCHANTCount: 3, $$hashKey: "object:317"} 
    2 : {_id: "2017-12-06", totalcount: Array(1), DRIVERCount: 1, $$hashKey: "object:318"} 
    3 : {_id: "2017-12-04", totalcount: Array(3), DRIVERCount: 1, MERCHANTCount: 2, USERCount: 1, …} 

ich die gleiche Operation sein wollen durchgeführt mit Map/Filter oder Reduce-Methode.

+0

Warum genau wollen Sie 'map' /' reduce' verwenden, wie würden Sie Ihren Code verbessern? – Bergi

+1

@Bergi Ich weiß nicht, ob es meinen Code verbessern wird oder nicht bro wollte einfach nur Funktionen höherer Ordnung mit diesem so – Kannan

+0

Ich werde nicht wirklich, wie Ihr Code hat eine Nebenwirkung (Hinzufügen von Feldern zu den vorhandenen Objekten). Sie können es jedoch vereinfachen, indem Sie "for ... of" -Schleifen verwenden und dynamische Eigenschaftsnamen anstelle dieser "if" -Kaskade verwenden, wie in NinaScholz 'Antwort. – Bergi

Antwort

4

Ziemlich einfach mit ES6 Syntax und Karte/reduzieren, Ful l Funktions Schnipsel:

var arr = [{_id:"2017-12-08",totalcount:[{orderstatus:"MERCHANT",total:1}]},{_id:"2017-12-02",totalcount:[{orderstatus:"USER",total:2}]},{_id:"2017-12-06",totalcount:[{orderstatus:"DRIVER",total:1}]},{_id:"2017-12-07",totalcount:[{orderstatus:"MERCHANT",total:3}]},{_id:"2017-12-04",totalcount:[{orderstatus:"DRIVER",total:1},{orderstatus:"MERCHANT",total:2},{orderstatus:"USER",total:1}]}]; 
 

 
    const result = arr 
 
     .map(({_id, totalcount}) => ({ 
 
     totalcount, 
 
     _id, 
 
     ...totalcount.reduce((acc, {orderstatus, total}) => 
 
      ({...acc, [`${orderstatus}count`]: total}), {}) 
 
     })) 
 

 
console.log(result)

https://jsbin.com/kiwalozuxa/edit?html,js,console,output

EDIT: The spread operator (...) wird verwendet, um die Tasten/Werte eines objet (oder Werte eines Arrays) zu verbreiten. totcalcount.reduce hier zurückgeben ein Objekt:

{ 
    x: "x", 
    y: "y" 
} 

Da es in Javascript technisch nicht gültig ist eine objet dieser Form zu haben:

{ 
    a: "a", 
    b: "b", 
    { 
    x: "x", 
    y: "y" 
    } 
} 

ich die Ausbreitung Operator mein „Kind“ Objekt verwenden, in die übergeordnete zu fusionieren objet und produzieren:

{ 
    a: "a", 
    b: "b", 
    x: "x", 
    y: "y" 
} 

Ich benutze die gleiche Technik in der Reduzierung.

+0

Das ist genial funktioniert perfekt, aber ich konnte Ihren Code nicht verstehen, was ist das ... vor der Gesamtzahl? Kannst du das bitte erklären? Danke – Kannan

+0

ja sicher, bearbeite ich baldmöglichst – Epitouille

0

Sie könnten es auf zwei verschachtelte forEach-Schleifen verkleinern und orderstatus als Teil der neuen Eigenschaft nehmen.

var totalReference = { MERCHANT: 'MERCHANTCount', USER: 'USERCount', DRIVER: 'DRIVERCount' }, 
 
    array = [{ _id: "2017-12-08", totalcount: [{ orderstatus: "MERCHANT", total: 1 }] }, { _id: "2017-12-02", totalcount: [{ orderstatus: "USER", total: 2 }] }, { _id: "2017-12-06", totalcount: [{ orderstatus: "DRIVER", total: 1 }] }, { _id: "2017-12-07", totalcount: [{ orderstatus: "MERCHANT", total: 3 }] }, { _id: "2017-12-04", totalcount: [{ orderstatus: "DRIVER", total: 1 }, { orderstatus: "MERCHANT", total: 2 }, { orderstatus: "USER", total: 1 }] }]; 
 

 
array.forEach(function (o) { 
 
    o.totalcount.forEach(function (p) { 
 
     if (totalReference[p.orderstatus]) { 
 
      o[totalReference[p.orderstatus]] = p.total; 
 
     } 
 
    }); 
 
}); 
 

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

1

Ich würde reduzieren und weisen Sie

let res = arr.reduce((a, b) => { 
    let totals = b.totalcount.reduce((x, y) => { 
     x[y.orderstatus + 'Count'] = (x[y.orderstatus + 'Count'] || 0) + y.total; 
     return x; 
    }, {}); 
    return a.concat(Object.assign(b, totals)); 
}, []); 

var arr = [{_id:"2017-12-08",totalcount:[{orderstatus:"MERCHANT",total:1}]},{_id:"2017-12-02",totalcount:[{orderstatus:"USER",total:2}]},{_id:"2017-12-06",totalcount:[{orderstatus:"DRIVER",total:1}]},{_id:"2017-12-07",totalcount:[{orderstatus:"MERCHANT",total:3}]},{_id:"2017-12-04",totalcount:[{orderstatus:"DRIVER",total:1},{orderstatus:"MERCHANT",total:2},{orderstatus:"USER",total:1}]}]; 
 

 
let res = arr.reduce((a, b) => { 
 
    let totals = b.totalcount.reduce((x, y) => { 
 
     x[y.orderstatus + 'Count'] = (x[y.orderstatus + 'Count'] || 0) + y.total; 
 
     return x; 
 
    }, {}); 
 
    return a.concat(Object.assign(b, totals)); 
 
}, []); 
 

 
console.log(res);

Verwandte Themen