合并具有相同 id 的数组
Merging arrays with same id
这是我的 Array。
我想合并具有相同 _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
});
}
});
但是没有删除重复的数组,而是说错误为未定义推送。我犯了什么错误,我该如何解决?
预期的输出应该是这样的:
{
"_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
使用散列 table 并检查是否存在具有该散列的对象。如果不是用实际元素做一个新的散列,否则在散列的对象上添加说明table并拼接数组。
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; }
将 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);
我提出了一个简单明了的解决方案。问题实际上是 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);
这是我的 Array。
我想合并具有相同 _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
});
}
});
但是没有删除重复的数组,而是说错误为未定义推送。我犯了什么错误,我该如何解决?
预期的输出应该是这样的:
{
"_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
使用散列 table 并检查是否存在具有该散列的对象。如果不是用实际元素做一个新的散列,否则在散列的对象上添加说明table并拼接数组。
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; }
将 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);
我提出了一个简单明了的解决方案。问题实际上是 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);