合并具有相同ID的数组

时间:2017-01-05 08:38:50

标签: javascript arrays node.js push

这是我的Array

我想合并具有相同_id的数组,即86ded3fdfc5f92724491f82我该怎么做? 我这样做是为了创建数组。

query()

我试图删除像这样的复制品

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

注意:我想在javascript中执行

3 个答案:

答案 0 :(得分:1)

您可以为_id使用哈希表,并检查是否存在具有哈希的对象。如果没有使用实际元素创建新哈希,否则将perscriptions添加到哈希表的对象并拼接数组。



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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用唯一的prescription_data键将_id缩减为对象地图,然后返回该对象的值。

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

答案 2 :(得分:1)

我建议一个干净简单的解决方案。问题实际上是,Array.indexOf不会在对象上工作,所以对于&#34;过滤器来说,这是一个小帮手。方法,它使用&#34; _id&#34;字段作为药物对象的id。 适用于ECMAScript 5.

&#13;
&#13;
protocol V {}

protocol P {
  var v: V? { get }
}
&#13;
&#13;
&#13;