2017-01-05 23 views
1

私のArrayです。同じIDを持つ配列のマージ

同じ_idを持つ配列、つまり86ded3fdfc5f92724491f82をマージしたいのですがどうすればいいですか? このようにして配列を作成しています。

dinnerDrug.push({ 
    '_id': value._id, 
    'name': value.medicine_name, 
    'count': value.dose_dinner_count, 
    'type': value.medicine_type, 
    'consume': value.dose_dinner_consume, 
    'comment': value.medicine_comment 
}); 

dinnerArray.push({ 
    '_id': value.doctor_id, 
    'doctor_name': value.doctor_name, 
    'doctor_dept': 'Cardiologist', 
    'prescription': dinnerDrug 
}); 

私はこの

morningArray.forEach(function(val) { 
    if (val._id == value.doctor_id) { 
     morningArray.prescription.push(morningDrug) 
    } else { 
      morningArray.push({ 
      '_id': value.doctor_id, 
      'doctor_name': value.doctor_name, 
      'doctor_dept': 'Cardiologist', 
      'prescription': morningDrug 
     }); 
    } 
}); 

ようdupliateを削除しようとしましたが、重複配列ではなく、それがプッシュなどのエラーが定義されていないと言い、削除されません。私は何をしているのですか?

予想される出力は次のようにする必要があります:

{ 
    "_id": "586ded3fdfc5f92724491f82", 
    "doctor_name": "asd asd", 
    "doctor_dept": "Cardiologist", 
    "prescription": [ 
     { 
      "_id": "586dfdbe98c23d1a200cfb3f", 
      "name": "ALPHACAINE N, solution injectable à usage dentaire", 
      "count": "1", 
      "type": "0", 
      "consume": "0", 
      "comment": "test" 
     }, 
     { 
      "_id": "586dfda498c23d1a200cfb3b", 
      "name": "ALPHACAINE N, solution injectable à usage dentaire", 
      "count": "1", 
      "type": "0", 
      "consume": "0", 
      "comment": "test" 
     } 
    ] 
} 

注:私はあなたが_idのハッシュテーブルを使用してオブジェクトをかどうかを確認できjavascriptの

+1

と 作品これは役立つかもしれない:http://codereview.stackexchange.com/questions/60128/removing-duplicates-from-an-array-quickly – Rajesh

+0

とき重複がないか確認してください。この行 'morningArray.prescription.push(morningDrug)'は 'val.prescription.push(morningDrug)'でなければなりません。 'morningArray'には' prescription'プロパティはありませんか? –

+0

私はあまりにもそれを試しました.. 'val.prescription.push(morningDrug)' stilは配列を空にします –

答えて

1

にこののみをしたいですハッシュが存在する。実際の要素で新しいハッシュを作成しない場合は、ハッシュテーブルのオブジェクトに記述を追加し、配列をスプライスします。そのオブジェクトの値を返す、独自の_idキーを持つオブジェクト・マップにprescription_dataを削減

var data = { success: "1", prescription_data: [{ _id: "586c95a4ce997012a44f777c", doctor_name: "new doctor", doctor_dept: "Cardiologist", prescription: [{ _id: "586c9f48fa0e603670cb01ae", name: "ASCOFER 33 mg, gélule", count: "1", type: "0", consume: "0", comment: "asdfd" }] }, { _id: "586ded3fdfc5f92724491f82", doctor_name: "asd asd", doctor_dept: "Cardiologist", prescription: [{ _id: "586dfda498c23d1a200cfb3b", name: "ALPHACAINE N, solution injectable à usage dentaire", count: "1", type: "0", consume: "0", comment: "test" }] }, { _id: "586ded3fdfc5f92724491f82", doctor_name: "asd asd", doctor_dept: "Cardiologist", prescription: [{ _id: "586dfdbe98c23d1a200cfb3f", name: "ALPHACAINE N, solution injectable à usage dentaire", count: "1", type: "0", consume: "0", comment: "test" }] }] }, 
 
    hash = Object.create(null), 
 
    i = 0; 
 

 
while (i < data.prescription_data.length) { 
 
    if (hash[data.prescription_data[i]._id]) { 
 
     hash[data.prescription_data[i]._id].prescription = hash[data.prescription_data[i]._id].prescription.concat(data.prescription_data[i].prescription); 
 
     data.prescription_data.splice(i, 1); 
 
     continue; 
 
    } 
 
    hash[data.prescription_data[i]._id] = data.prescription_data[i]; 
 
    i++; 
 
} 
 

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

+0

私は今同じものを試してみましょう –

1

var data = {"success":"1","prescription_data":[{"_id":"586c95a4ce997012a44f777c","doctor_name":"new doctor","doctor_dept":"Cardiologist","prescription":[{"_id":"586c9f48fa0e603670cb01ae","name":"ASCOFER 33 mg, gélule","count":"1","type":"0","consume":"0","comment":"asdfd"}]},{"_id":"586ded3fdfc5f92724491f82","doctor_name":"asd asd","doctor_dept":"Cardiologist","prescription":[{"_id":"586dfda498c23d1a200cfb3b","name":"ALPHACAINE N, solution injectable à usage dentaire","count":"1","type":"0","consume":"0","comment":"test"}]},{"_id":"586ded3fdfc5f92724491f82","doctor_name":"asd asd","doctor_dept":"Cardiologist","prescription":[{"_id":"586dfdbe98c23d1a200cfb3f","name":"ALPHACAINE N, solution injectable à usage dentaire","count":"1","type":"0","consume":"0","comment":"test"}]}]}; 
 

 
data.prescription_data = Object.values(data.prescription_data.reduce(function (aggr, item) { 
 
    if(aggr[item._id]){ 
 
     aggr[item._id].prescription = aggr[item._id].prescription.concat(item.prescription); 
 
    } else { 
 
     aggr[item._id] = item; 
 
    } 
 
    return aggr; 
 
},{})); 
 

 
console.log(data);

1

私は清潔で簡単な解決策を提案します。実際問題はArray.indexOfはオブジェクトに対して機能しないため、 "filter"メソッドのヘルパーが少しあります。このメソッドは、 "_id"フィールドをドラッグオブジェクトのIDとして使用します。 ECMAScriptの5

var srcObj = {"success":"1","prescription_data":[{"_id":"586c95a4ce997012a44f777c","doctor_name":"new doctor","doctor_dept":"Cardiologist","prescription":[{"_id":"586c9f48fa0e603670cb01ae","name":"ASCOFER 33 mg, gélule","count":"1","type":"0","consume":"0","comment":"asdfd"}]},{"_id":"586ded3fdfc5f92724491f82","doctor_name":"asd asd","doctor_dept":"Cardiologist","prescription":[{"_id":"586dfda498c23d1a200cfb3b","name":"ALPHACAINE N, solution injectable à usage dentaire","count":"1","type":"0","consume":"0","comment":"test"}]},{"_id":"586ded3fdfc5f92724491f82","doctor_name":"asd asd","doctor_dept":"Cardiologist","prescription":[{"_id":"586dfdbe98c23d1a200cfb3f","name":"ALPHACAINE N, solution injectable à usage dentaire","count":"1","type":"0","consume":"0","comment":"test"}]}]}; 
 

 
var indexOfId = function(arr, obj){ 
 
    for(var objIdx in arr){ 
 
     if(arr[objIdx]._id === obj._id) return objIdx; 
 
    } 
 
} 
 

 
srcObj.prescription_data = srcObj.prescription_data.filter((o, i, a) => indexOfId(a, o) == i); 
 

 
console.log(srcObj);

関連する問題