2017-08-07 51 views
0

我的angular4項目中有這個json對象。我想根據公共屬性對該對象進行分組或合併。如何根據angular4項目中的公共屬性對我的json對象進行分組或合併

如何基於fixture_desc合併這些對象?我想顯示爲fixture_desc =「百事可樂冷卻器」,並在其下的所有項目。

"items": [ 
       { 
       "barcode": "052000324815", 
       "price": 2, 
       "taxable": false, 
       "description": "Gatorade Cool Blue", 
       "tax_rate": 0, 
       "inventory": 8, 
       "fixture_desc": "Pepsi Cooler" 

       }, 
       { 
       "barcode": "052000328660", 
       "price": 2, 
       "taxable": false, 
       "description": "Gatorade Fruit Punch ", 
       "tax_rate": 0, 
       "inventory": 8, 
       "fixture_desc": "Pepsi Cooler", 
       "min": 2, 
       "max": 8, 
       "_id": "58feb29a3a5c560011b1b96c" 
       }, 
       { 
       "barcode": "052000328684", 
       "price": 2, 
       "taxable": false, 
       "description": "Gatorade Lemon Lime ", 
       "tax_rate": 0, 
       "inventory": 4, 
       "fixture_desc": "Pepsi Cooler", 
       "min": 1, 
       "max": 4, 

       } 
     ] 

我想使組織顯示我的數據。我預期的結果是這樣的:

"items": [ 
{ 
    "name": "Pepsi Cooler」, 
    "items": [ 
    { 
      "barcode": "052000324815", 
      "price": 2, 
      "taxable": false, 
      "description": "Gatorade Cool Blue", 
      "tax_rate": 0, 
      "inventory": 8, 
      "fixture_desc": "Pepsi Cooler" 
     }, 
     { 
      "barcode": "052000328660", 
      "price": 2, 
      "taxable": false, 
      "description": "Gatorade Fruit Punch ", 
      "tax_rate": 0, 
      "inventory": 8, 
      "fixture_desc": "Pepsi Cooler" 

     } 
    ] 
} 
{ 
    "name": 「Cook Cooler」, 
    "items": [ 
    { 
      "barcode": "052000324815", 
      "price": 2, 
      "taxable": false, 
      "description": "Gatorade Cool Blue", 
      "tax_rate": 0, 
      "inventory": 8, 
      "fixture_desc": "Cook Cooler 
     }, 
     { 
      "barcode": "052000328660", 
      "price": 2, 
      "taxable": false, 
      "description": "Gatorade Fruit Punch ", 
      "tax_rate": 0, 
      "inventory": 8, 
      "fixture_desc": "Cook Cooler 
     } 
    ] 
} 
] 
+1

你能提供預期的結果嗎? – Myonara

+0

@Myonara我更新了我的問題,請檢查它。謝謝。 –

回答

1

我解決我的問題。我發佈它,如果它幫助任何人。 這裏我的數組:

var myobj={"items": [ 
         { 
         "barcode": "052000324815", 
         "description": "Gatorade Cool Blue", 
         "fixture_desc": "Pepsi Cooler" 
         }, 
         { 
         "barcode": "052000328660", 
         "description": "Gatorade Fruit Punch ", 
         "fixture_desc": "Pepsi Cooler", 
         }, 
         { 
         "barcode": "052000328684", 
         "description": "Gatorade Lemon Lime ", 
         "fixture_desc": "Pepsi Cooler", 
         } 
       ] 
    } 
    and this is my solution: 
    result:any[] = []; 
    finalResult(myObj:any){ 
       var Obj = myObj; 
       var groups:any[]=[]; 
    for (let a of Obj) { 
     groups[a.fixture_desc] = groups[a.fixture_desc] || { 
         name:a.fixture_desc, 
         items:[], 
         isfixtureExpanded:false 
        }; 
        groups[a.fixture_desc].items.push({ 

          barcode: a.barcode, 
          description: a.description, 
          fixture_desc:a.fixture_desc, 

        }); 
       } 

     for (var key in groups){ 

      this.result.push(groups[key]); 
     } 
       } 

console.log(this.result); 
    } 
0

而不是問你能告訴我們什麼你做來實現它的代碼,因此不代碼生成器。但無論如何,這裏的代碼可以做你想要的。

function group(array, prop) { 
var result = []; 
    array.forEach(function(val) { 
     var found = false; 
     for(var i=0;i<result.length;i++) { 
      if(result[i].name === val[prop]) { 
       result[i].items.push(val); 
       found = true; 
      } 
     } 
    }); 
} 

稱其爲

var finalResult = group(items,'fixture_desc'); 
相關問題