我有一个像这样的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从数据库获取此数据,我只是从同一个集合中获取了两次数据,现在我想使最终的json为unique(_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)
            })
        })
    })
}

最佳答案

您可以为此使用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);

关于javascript - Node js中的唯一json数据对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50228626/

10-09 23:48
查看更多