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)
            })
        })
    })
}
user8576367
  • 207
  • 3
  • 14
  • show us some code that you had tried. – Dean May 08 '18 at 07:54
  • what you want to achieve? do you want to add this objects in array? – PPL May 08 '18 at 07:54
  • I think this a duplicate of https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array, check it out – tjadli May 08 '18 at 07: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 – Sabik May 08 '18 at 08: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? – M. F. May 08 '18 at 08:05
  • @M.F. im just want to get data like, data with same object id should not be repeated – user8576367 May 08 '18 at 08:27
  • @Dean updated now – user8576367 May 08 '18 at 08:41

3 Answers3

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);
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
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);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • 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. (/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) – user8576367 May 08 '18 at 08:44
  • Added ES5 Code snippet – Hassan Imam May 08 '18 at 08:48
  • Object.values is not a function – user8576367 May 08 '18 at 08:56
  • Please use polyfill to add support of `Obejct.values()` in the older browser. – Hassan Imam May 08 '18 at 08:58
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>
M. F.
  • 1,654
  • 11
  • 16