1

i have a json data like this,

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 } ]

where _id: 5abb46b060808f3a2096f91d is repeated twice, but i want this data object to be contain unique _id, please help me to solve

update:

This is my API to get this data from db, im just getting data from same collection twice, and now i want to make that final json as 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)
            })
        })
    })
}
7
  • show us some code that you had tried. Commented May 8, 2018 at 7:54
  • what you want to achieve? do you want to add this objects in array? Commented May 8, 2018 at 7:54
  • I think this a duplicate of stackoverflow.com/questions/9229645/…, check it out Commented May 8, 2018 at 7:59
  • It looks like your data was fetched from DB. The best way will be prepare data for UI on the backend. Maybe in your data scheme there is a mistake if you received duplicates Commented May 8, 2018 at 8:04
  • 1
    It is unclear what you want to achieve: do you want to remove duplicates in your data, so that each object is shown only once, making the _id unique, or do you want to assign a unique _id to the duplicates? Commented May 8, 2018 at 8:05

3 Answers 3

1

You can use a Map object for this purpose:

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

Or enumerate:

patientMap.forEach((row) => {
    console.log(row);
});

And to turn it back into an Array:

let newArray = [...patientMap.values()]
console.log(newArray);
Sign up to request clarification or add additional context in comments.

5 Comments

Do you see duplicates in the newArray array?
Can you try the edited code.. e.g. change this line to if (!patientMap.has(row._id.toString()) patientMap.set(row._id.toString(), row);
Cool, the MongoDB objectId just needs to be converted to a string to make it a key in a map!
now using babel, i converted that code to es5, so it is working now
Great! I suspect the version of Node.js used did not like the spread (...) operator.
1

You can use array#reduce with Object.values() to group your data based on _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 code

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

4 Comments

SyntaxError: Unexpected token ... at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:374:25) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at Object.<anonymous> (/home/logakrishnan/Desktop/project/hospital-api/server/routes/billing.route.js:2:19) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10)
Added ES5 Code snippet
Object.values is not a function
Please use polyfill to add support of Obejct.values() in the older browser.
0

If you use a library like underscore.js it becomes a one-line solution, see snippet below. You basically tell underscore to return a new array containing only the unique items, with unique criteria being the _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);
<script src="http://underscorejs.org/underscore-min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.