简体   繁体   English

合并具有相同ID的数组

[英]Merging arrays with same id

Here's my Array . 这是我的数组

I want to merge the arrays that having the same _id ie, 86ded3fdfc5f92724491f82 How can i do this ? 我想合并具有相同_id的数组,即86ded3fdfc5f92724491f82我该怎么做? i am doing like this to create array. 我这样做是为了创建数组。

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

I tried to remove the dupliate like this 我试图删除这样的重复

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

But the duplicate array is not removed, instead it says the error as push is undefined. 但是没有删除重复的数组,而是说由于未定义push而导致的错误。 What mistake am I am doing and how can I fix this? 我在做什么错误,该如何解决?

The expected output should be like this: 预期的输出应如下所示:

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

Note : I want to do this only in javascript 注:我想这样做只是在javascript

You could use a hash table for _id and check if an object with the hash exists. 您可以将哈希表用于_id并检查是否存在带有哈希的对象。 If not make a new hash with the actual element, otherwise add the perscriptions to the object of the hash table and splice the array. 如果不使用实际元素进行新的哈希处理,则将说明添加到哈希表的对象并拼接数组。

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

Reducing prescription_data to a Object Map with unique _id keys, then returning values of that Object. 使用唯一的_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"}]}]}; 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); 

I propose a clean and easy solution. 我提出了一个干净而简单的解决方案。 The problem is in fact, that Array.indexOf won't work on objects, so there's a little helper for the "filter" method, which uses the "_id" field as the id of an drug object. 问题实际上是Array.indexOf无法在对象上运行,因此“ filter”方法有一个小帮手,该方法使用“ _id”字段作为药物对象的id。 Works with ECMAScript 5. 与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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM