合并具有相同ID的数组

这是我的arrays 。

我想要合并具有相同_id的数组,即86ded3fdfc5f92724491f82我该怎么做? 我正在这样做来创build数组。

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 }); } }); 

但重复的数组不会被删除,而是说错误为推送未定义。 我在做什么错误,我该如何解决这个问题?

预期的输出应该是这样的:

 { "_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" } ] } 

注:我只想在JavaScript中做到这一点

您可以使用散列表_id并检查是否存在与哈希对象。 如果不是用实际的元素创build一个新的散列值,否则将散列元素添加到散列表的对象并拼接数组。

 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; } 

使用唯一的_id键将prescription_data减less为Object Map,然后返回该Object的值。

 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); 

我提出一个干净而简单的解决scheme。 问题是,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);