2017-08-11 1 views
2

私は唯一の違いは、オブジェクトの私の配列は、2つの以上の要素を持っていることであるhereグループの配列項目は、私は2つの以上の要素

解決しようとしている問題の種類については、この解決策を見つけた となります結果は望んでいた誰かしてくださいできソリューションに似ていますが、すべての要素

{ 
    "group": "one", 
    "color": ["red", "green", "black"], 
    "size": ["big"], 
    "date": ["11/08/2018"] 
} 

でだから私はすべての私の値を表示するために取得する.map()を繰り返してきたが、私は、私はいけないと感じて...
より簡単でより良いオプションで私を助けてください?後で追加されたコードについて

var db = [{"Record":{"id":"26","cost_center":"15073 DC1 M8 - Filmatic","batch_no":"367746","item_code":"12583","description":"LF Fruited Guava (2x6)x15"}},{"Record":{"id":"29","cost_center":"15073 DC1 M8 - Filmatic","batch_no":"367749","item_code":"12583","description":"LF Fruited Guava (2x6)x15"}},{"Record":{"id":"36","cost_center":"15093 DC1 M10 - CornerPot Machi","batch_no":"367756","item_code":"12256","description":"PROMO CP LF SaltedCar w H"}}]; 
 

 
var myArray = []; 
 
for (var i in db) { 
 
    if (db.hasOwnProperty(i)) { 
 
     myArray.push(db[i].Record); 
 
    } 
 
} 
 

 
var res = myArray.reduce(function(res, elem) { 
 
    if (res.indexOf(elem.cost_center) === -1) { 
 
     res.push(elem.cost_center); 
 
    } 
 
    return res; 
 
}, []).map(function(machine) { 
 
    return { 
 
     cost_center: machine, 
 
     batch_no: myArray.filter(function(_el) { 
 
      return _el.cost_center === machine; 
 
     }).map(function(_el) { return _el.batch_no; }), 
 
     item_code: myArray.filter(function(_el) { 
 
      return _el.cost_center === machine; 
 
     }).map(function(_el) { return _el.item_code; }), 
 
     description: myArray.filter(function(_el) { 
 
      return _el.cost_center === machine; 
 
     }).map(function(_el) { return _el.description; }) 
 
    } 
 
}); 
 

 
console.log(res);

+1

どこから 'size'と' date'、などの追加のプロパティを取得できますか? –

+0

お客様から@NinaScholz – Jonathan

+0

okですが、データ構造はありますか? –

答えて

1

、あなたは、あなたが同じcost_centerですべてのオブジェクトを収集し、batch_noように、プロパティの値を収集するための別の配列を使用する場合、ハッシュ・テーブルを使用することができitem_codeおよびdescription

var db = [{ Record: { id: "26", cost_center: "15073 DC1 M8 - Filmatic", batch_no: "367746", item_code: "12583", description: "LF Fruited Guava (2x6)x15" } }, { Record: { id: "29", cost_center: "15073 DC1 M8 - Filmatic", batch_no: "367749", item_code: "12583", description: "LF Fruited Guava (2x6)x15" } }, { Record: { id: "36", cost_center: "15093 DC1 M10 - CornerPot Machi", batch_no: "367756", item_code: "12256", description: "PROMO CP LF SaltedCar w H" } }], 
 
    keys = ["batch_no", "item_code", "description"], 
 
    hash = Object.create(null), 
 
    result = []; 
 

 
db.forEach(function (o) { 
 
    if (!hash[o.Record.cost_center]) { 
 
     hash[o.Record.cost_center] = { cost_center: o.Record.cost_center }; 
 
     keys.forEach(function (k) { 
 
      hash[o.Record.cost_center][k] = [o.Record[k]]; 
 
     }); 
 
     result.push(hash[o.Record.cost_center]); 
 
     return; 
 
    } 
 
    keys.forEach(function (k) { 
 
     hash[o.Record.cost_center][k].push(o.Record[k]); 
 
    }); 
 
}); 
 

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

+0

ありがとう、使いやすく編集が簡単です! – Jonathan

関連する問題