0
var values = [
    { name: 'Car',Data:'Required',value:'Vehicle' },
    { name: 'Auto',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Vehicle'},
    { name: 'Car',Data:'Required',value:'Vehicle' }
];

Duplicate object will be: { name: 'Car',Data:'Required',value:'Vehicle' }

Npsad
  • 508
  • 4
  • 12

3 Answers3

1

If you want to check all the properties of objects, you have to introduce a new function, and use it to filter your array:

var values = [
    { name: 'Car',Data:'Required',value:'Vehicle' },
    { name: 'Auto',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Different VL'},
    { name: 'Car',Data:'Required',value:'Vehicle' }
];

Object.compare = function (obj1, obj2) {
 //Loop through properties in object 1
 for (var p in obj1) {
  //Check property exists on both objects
  if (obj1.hasOwnProperty(p) !== obj2.hasOwnProperty(p)) return false;
 
  switch (typeof (obj1[p])) {
   //Deep compare objects
   case 'object':
    if (!Object.compare(obj1[p], obj2[p])) return false;
    break;
   //Compare function code
   case 'function':
    if (typeof (obj2[p]) == 'undefined' || (p != 'compare' && obj1[p].toString() != obj2[p].toString())) return false;
    break;
   //Compare values
   default:
    if (obj1[p] != obj2[p]) return false;
  }
 }
 
 //Check object 2 for any extra properties
 for (var p in obj2) {
  if (typeof (obj1[p]) == 'undefined') return false;
 }
 return true;
};


var doubles = values.filter((x,i,a) =>a.slice(0,i).find(y=>Object.compare(x,y)))

console.log(doubles)
djcaesar9114
  • 1,880
  • 1
  • 21
  • 41
  • This won't work. Check [**this fiddle**](https://jsfiddle.net/yteLfu5c/). You're only ever checking if `name` is duplicate, whereas the OP specifically asked for a solution pointing to objects whose **all** values are duplicates (i.e. same as `DISTINCT` in SQL). Perhaps the question is a bit poorly worded. – gyohza Jun 03 '20 at 05:53
  • OK, I changed my code. Please reconsider your downvote. – djcaesar9114 Jun 03 '20 at 06:40
  • Nice! :) I did revoke it, but I really need to hit it, right now (4:15 AM) - so I can't validate your code. Maybe I'll check it out later. – gyohza Jun 03 '20 at 07:16
  • OK I hope you won't forget it ;) – djcaesar9114 Jun 03 '20 at 11:31
  • 1
    Oh! [Tested it again](https://jsfiddle.net/gyohza/vxbrgL47/2/) - I love what you did there! Nice recursion. Earned an upvote. ;) – gyohza Jun 03 '20 at 12:21
1

You can check if an array contains the same item on a different index and by that, understand that there is a duplicate:

const values = [
    { name: 'Car',Data:'Required',value:'Vehicle' },
    { name: 'Auto',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Vehicle'},
    { name: 'Car',Data:'Required',value:'Vehicle' }
];

const duplicates = values.filter((item, idx) => values.findIndex(i => item.name === i.name && item.Data === i.Data && item.value === i.value) !== idx);

console.log(duplicates);
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • its returning empty even if duplicate row exists – Npsad Jun 03 '20 at 06:23
  • @Npsad - Running my code snippet shows it works. Paste your array here.. – Amir Popovich Jun 03 '20 at 06:30
  • const array = this.chatForm.controls['conversation']; let temp=[]; temp.push(array.value) const duplicates = temp.filter((item, idx) => temp.findIndex(i => item.createdBranch === i.createdBranch && item.responseType === i.responseType && item.query === i.query) !== idx); console.log(duplicates); dynamic values from form am getting and pushing to temp – Npsad Jun 03 '20 at 06:32
  • That is not an array. – Amir Popovich Jun 03 '20 at 06:40
  • Yeah i got it !!! this solution Works Fine a small little tweak from my codebase was needed thanks!!!! – Npsad Jun 03 '20 at 06:48
0

Working ahead from the answer in: https://stackoverflow.com/a/840808/11057988

var findDuplicates = (arr) => {
  let sorted_arr = arr.slice().sort(sortFunc); // You can define the comparing function here. 
  // JS by default uses a crappy string compare.
  // (we use slice to clone the array so the
  // original array won't be modified)
  let results = [];
  for (let i = 0; i < sorted_arr.length - 1; i++) {
    if (sortFunc(sorted_arr[i + 1], sorted_arr[i]) === 0) {
      results.push(sorted_arr[i]);
    }
  }
  return results;
}

var values = [
    { name: 'Car',Data:'Required',value:'Vehicle' },
    { name: 'Auto',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Vehicle'},
    { name: 'Car',Data:'Required',value:'Vehicle' }
];

var sortFunc = (a, b) => {
  return   (a.name !== b.name) ? a.name.localeCompare(b.name)
         : (a.Data !== b.Data) ? a.Data.localeCompare(b.Data)
         : a.value.localeCompare(b.value);
};

console.log(`The duplicates are ${JSON.stringify(findDuplicates(values))}`);
// prints The duplicates are [{"name":"Car","Data":"Required","value":"Vehicle"}]

Couple of differences from original answer:
1. The sortFunc is defined as per your data.
2. Since your input is array of objects, the sortFunc is reused for equality check.

Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32