我有像这样的json数据,
var data = [ { _id: 5abb46b060808f3a2096f91d,
patient_name: 'krishnaaaannnn',
gender: 'male',
charge_id: 5ab243ac73959deb3ad79fec,
description: 'suffering from pain',
hospital_id: 5aa92df9ec6b3cc78ff88afe,
doctor_id: 5af142f879f06c22f5359be3 },
{ _id: 5af143a779f06c22f5359be4,
patient_name: 'Rajesh',
gender: 'male',
description: 'suffering from fever',
charge_id: 5aaa628e9ae2a2a642b7495c,
hospital_id: 5aa92df9ec6b3cc78ff88afe,
doctor_id: 5af142f879f06c22f5359be3 },
{ _id: 5af144685f2f292b2cc3af6d,
patient_name: 'krishh',
gender: 'male',
description: 'suffering from fever',
charge_id: 5aaa628e9ae2a2a642b7495c,
hospital_id: 5aa92df9ec6b3cc78ff88afe,
doctor_id: 5af142f879f06c22f5359be3 },
{ _id: 5abb46b060808f3a2096f91d,
patient_name: 'krishnaaaannnn',
gender: 'male',
charge_id: 5ab243ac73959deb3ad79fec,
description: 'suffering from pain',
hospital_id: 5aa92df9ec6b3cc78ff88afe,
doctor_id: 5af142f879f06c22f5359be3 } ]
其中_id: 5abb46b060808f3a2096f91d
重复两次,但我希望此数据对象包含unique _id
,请帮我解决
更新
这是我的API
从db获取此数据,我只是从同一个集合中获取数据两次,现在我想将最终的json设为唯一(_id)
function billing_read_all(req, res) {
var auth_data = res.locals.result;
var searchParam = { token: auth_data[0].token }
model.find("doctor", searchParam, function (doctor_data) {
var searchParam1 = { doctor_id: doctor_data[0]._id }
model.find("patient", searchParam1, function (patient_data) {
var searchParam2={charge_id:ObjectId("5ab243ac73959deb3ad79fec")}
model.find("patient", searchParam2, function (patient_data2) {
var data = patient_data.concat(patient_data2)
console.log(_.uniq(data))
res.send(_.uniq(data))
// res.send(patient_data)
})
})
})
}
答案 0 :(得分:1)
您可以将Map对象用于此目的:
var data = [ { _id: '5abb46b060808f3a2096f91d',
patient_name: 'krishnaaaannnn',
gender: 'male',
charge_id: '5ab243ac73959deb3ad79fec',
description: 'suffering from pain',
hospital_id: '5aa92df9ec6b3cc78ff88afe',
doctor_id: '5af142f879f06c22f5359be3' },
{ _id: '5af143a779f06c22f5359be4',
patient_name: 'Rajesh',
gender: 'male',
description: 'suffering from fever',
charge_id: '5aaa628e9ae2a2a642b7495c',
hospital_id: '5aa92df9ec6b3cc78ff88afe',
doctor_id: '5af142f879f06c22f5359be3' },
{ _id: '5af144685f2f292b2cc3af6d',
patient_name: 'krishh',
gender: 'male',
description: 'suffering from fever',
charge_id: '5aaa628e9ae2a2a642b7495c',
hospital_id: '5aa92df9ec6b3cc78ff88afe',
doctor_id: '5af142f879f06c22f5359be3' },
{ _id: '5abb46b060808f3a2096f91d',
patient_name: 'krishnaaaannnn',
gender: 'male',
charge_id: '5ab243ac73959deb3ad79fec',
description: 'suffering from pain',
hospital_id: '5aa92df9ec6b3cc78ff88afe',
doctor_id: '5af142f879f06c22f5359be3' } ];
var patientMap = new Map();
data.forEach(row => {
if (!patientMap.has(row._id.toString())) patientMap.set(row._id.toString(), row);
});
// One entry per unique ID now
for (var value of patientMap.values()) {
console.log(value);
};
或枚举:
patientMap.forEach((row) => {
console.log(row);
});
并将其变回数组:
let newArray = [...patientMap.values()]
console.log(newArray);
答案 1 :(得分:1)
您可以array#reduce
与Object.values()
一起使用_id
对数据进行分组。
var data = [ { _id: '5abb46b060808f3a2096f91d', patient_name: 'krishnaaaannnn', gender: 'male', charge_id: '5ab243ac73959deb3ad79fec', description: 'suffering from pain', hospital_id: '5aa92df9ec6b3cc78ff88afe', doctor_id: '5af142f879f06c22f5359be3' }, { _id:'5af143a779f06c22f5359be4', patient_name: 'Rajesh', gender: 'male', description: 'suffering from fever', charge_id: '5aaa628e9ae2a2a642b7495c', hospital_id: '5aa92df9ec6b3cc78ff88afe', doctor_id: '5af142f879f06c22f5359be3' }, { _id: '5af144685f2f292b2cc3af6d',patient_name: 'krishh', gender: 'male', description: 'suffering from fever', charge_id: '5aaa628e9ae2a2a642b7495c', hospital_id: '5aa92df9ec6b3cc78ff88afe', doctor_id: '5af142f879f06c22f5359be3' }, { _id: '5abb46b060808f3a2096f91d', patient_name: 'krishnaaaannnn',gender: 'male', charge_id: '5ab243ac73959deb3ad79fec', description: 'suffering from pain', hospital_id: '5aa92df9ec6b3cc78ff88afe', doctor_id: '5af142f879f06c22f5359be3' } ],
result = Object.values(data.reduce((r,o) => {
r[o._id] = r[o._id] || {...o};
return r;
},{}));
console.log(result);
ES5代码
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var data = [{ _id: '5abb46b060808f3a2096f91d', patient_name: 'krishnaaaannnn', gender: 'male', charge_id: '5ab243ac73959deb3ad79fec', description: 'suffering from pain', hospital_id: '5aa92df9ec6b3cc78ff88afe', doctor_id: '5af142f879f06c22f5359be3' }, { _id: '5af143a779f06c22f5359be4', patient_name: 'Rajesh', gender: 'male', description: 'suffering from fever', charge_id: '5aaa628e9ae2a2a642b7495c', hospital_id: '5aa92df9ec6b3cc78ff88afe', doctor_id: '5af142f879f06c22f5359be3' }, { _id: '5af144685f2f292b2cc3af6d', patient_name: 'krishh', gender: 'male', description: 'suffering from fever', charge_id: '5aaa628e9ae2a2a642b7495c', hospital_id: '5aa92df9ec6b3cc78ff88afe', doctor_id: '5af142f879f06c22f5359be3' }, { _id: '5abb46b060808f3a2096f91d', patient_name: 'krishnaaaannnn', gender: 'male', charge_id: '5ab243ac73959deb3ad79fec', description: 'suffering from pain', hospital_id: '5aa92df9ec6b3cc78ff88afe', doctor_id: '5af142f879f06c22f5359be3' }],
result = Object.values(data.reduce(function (r, o) {
r[o._id] = r[o._id] || _extends({}, o);
return r;
}, {}));
console.log(result);
答案 2 :(得分:0)
如果您使用像underscore.js这样的库,它将成为一个单行解决方案,请参阅下面的代码段。你基本上告诉下划线返回一个只包含唯一项的新数组,唯一的标准是_id。
var data = [ { _id: '5abb46b060808f3a2096f91d',
patient_name: 'krishnaaaannnn',
gender: 'male',
charge_id: '5ab243ac73959deb3ad79fec',
description: 'suffering from pain',
hospital_id: '5aa92df9ec6b3cc78ff88afe',
doctor_id: '5af142f879f06c22f5359be3' },
{ _id: '5af143a779f06c22f5359be4',
patient_name: 'Rajesh',
gender: 'male',
description: 'suffering from fever',
charge_id: '5aaa628e9ae2a2a642b7495c',
hospital_id: '5aa92df9ec6b3cc78ff88afe',
doctor_id: '5af142f879f06c22f5359be3' },
{ _id: '5af144685f2f292b2cc3af6d',
patient_name: 'krishh',
gender: 'male',
description: 'suffering from fever',
charge_id: '5aaa628e9ae2a2a642b7495c',
hospital_id: '5aa92df9ec6b3cc78ff88afe',
doctor_id: '5af142f879f06c22f5359be3' },
{ _id: '5abb46b060808f3a2096f91d',
patient_name: 'krishnaaaannnn',
gender: 'male',
charge_id: '5ab243ac73959deb3ad79fec',
description: 'suffering from pain',
hospital_id: '5aa92df9ec6b3cc78ff88afe',
doctor_id: '5af142f879f06c22f5359be3' } ];
var filteredData = _.uniq(data, (item) => item._id);
console.log(filteredData);
&#13;
<script src="http://underscorejs.org/underscore-min.js"></script>
&#13;