SA__
SA__

Reputation: 437

Merging arrays with same id

Here's my Array.

I want to merge the arrays that having the same _id i.e., 86ded3fdfc5f92724491f82 How can i do this ? 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. 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

Upvotes: 1

Views: 346

Answers (3)

Max
Max

Reputation: 1501

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. Works with 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);

Upvotes: 1

Bhabishya Kumar
Bhabishya Kumar

Reputation: 731

Reducing prescription_data to a Object Map with unique _id keys, then returning values of that 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);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386883

You could use a hash table for _id and check if an object with the hash exists. 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; }

Upvotes: 1

Related Questions