0

I am trying to find duplicate objects from array by camparing more than 1 field inside object.

I want to compare 2 fields of the object with the other objects of the same array.

I want to campare just name and lname ignoring the 3rd field link inside my object

  values = [{
    name: 'newton',
    lname: 'king',
    link: '123'
  }, {
    name: 'tom',
    lname: 'kurtz',
    link: '123'
  }, {
    name: 'newton',
    lname: 'king',
    link: '456'
  }, {
    name: 'jan',
    lname: 'heckal',
    link: '123'
  }]

How do I find and remove the duplicate objects with output that would look like this

  values = [{
    name: 'newton',
    lname: 'king',
    link: '123'
  }, {
    name: 'tom',
    lname: 'kurtz',
    link: '123'
  }, {
    name: 'jan',
    lname: 'heckal',
    link: '123'
  }]
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Manpreet Oberoi
  • 385
  • 3
  • 4
  • 13
  • 2
    This question has been answered many times before: http://stackoverflow.com/questions/16747798/delete-duplicate-elements-from-an-array – Nicholas Robinson Aug 31 '16 at 09:14
  • 1
    Welcome to SO. Please visit the [help] to see how and what to ask. HINT: Show effort and code and try to see if it has been answered before – mplungjan Aug 31 '16 at 09:14
  • 1
    [Just _how_ could you miss an answer...](https://www.google.rs/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=javascript%20remove%20duplicates%20from%20array) – Lazar Ljubenović Aug 31 '16 at 09:15
  • 1
    its really bad guys. i just joined the group and you guys giving me -1.. its hurts lot – Manpreet Oberoi Aug 31 '16 at 09:18
  • @NicholasRobinson that answer is helpful but you know.. i have to campare more than 1 object with each other – Manpreet Oberoi Aug 31 '16 at 09:19
  • 1
    So show you already know how to remove duplicates and then show and ask us about the code you wrote that fails to compare more than one object. The better the questions the better the answers and some good questions gets voted UP. – mplungjan Aug 31 '16 at 09:22
  • can you please tell me, how to campare more than 1 object – Manpreet Oberoi Aug 31 '16 at 09:24
  • Show me the objects and the output and the code you tried and I will gladly try to help you – mplungjan Aug 31 '16 at 09:26
  • https://jsfiddle.net/jehyc7vp/ check this out for exact answer – ajaykumar Aug 31 '16 at 09:27
  • i didn't find a way to campare muliple objects inside 1 array. can you please help me with this – Manpreet Oberoi Aug 31 '16 at 09:28
  • check the fiddle first. You can take only one key from the object and push in the array and check the subsequent objects using only one key. The entire array is filtered. – ajaykumar Aug 31 '16 at 09:31
  • Thanks @ajaykumar . thats quite useful but i have a question that. can you guide me. suppose we have 3 fields in one object and we want to campare only 2 fields with other objects.. is that possible? – Manpreet Oberoi Aug 31 '16 at 09:32
  • @ajaykumar you have to handle "newton first" and "newton second" one of which will disappear. http://stackoverflow.com/questions/19501441/remove-duplicate-objects-from-an-array-using-javascript – mplungjan Aug 31 '16 at 09:34
  • @ManpreetOberoi You need to pust that comment into the question with objects that reflect exactly what you want to do. The question is now different than the title. You possibly need to ask : _How to remove duplicate objects from an array based on more than one criteria_ test data: `{ name: 'newton', initial: 'a', lname: 'king'}, { name: 'tom', initial: 'b', lname: 'kurtz'}, { name: 'newton', initial: 'b', lname: 'king'},` – mplungjan Aug 31 '16 at 09:35
  • yes.. this is exactly my question man – Manpreet Oberoi Aug 31 '16 at 09:39
  • can you please help me with this – Manpreet Oberoi Aug 31 '16 at 09:39
  • I just did. http://stackoverflow.com/questions/19501441/remove-duplicate-objects-from-an-array-using-javascript - just add the fields that you need to compare on – mplungjan Aug 31 '16 at 09:40
  • that is not much helpful.. can you type the answer please... – Manpreet Oberoi Aug 31 '16 at 09:48
  • That is not what SO is for. If you update your question to show what you mean I may answer it. BUt you COULD take the answer I gave you, plug in your data, see if it worked and come back with the failing code to ask us a real question – mplungjan Aug 31 '16 at 09:51
  • okay i am updating my question – Manpreet Oberoi Aug 31 '16 at 09:51
  • Great. Please read this: http://stackoverflow.com/help/how-to-ask AND http://stackoverflow.com/help/mcve – mplungjan Aug 31 '16 at 09:52
  • Then visit the link I gave, here is a version with a LOT of comments: http://stackoverflow.com/a/37979616/295783 – mplungjan Aug 31 '16 at 09:57
  • i also found that... thanks lot... now you think.. my question was good question.... i hope you will take back your -1. thanks – Manpreet Oberoi Aug 31 '16 at 10:02

1 Answers1

0

This is almost a duplicate of Remove duplicate objects from an array using javascript

 var arrResult=[],
     arr = [{
    name: 'newton',
    lname: 'king',
    link: '123'
  }, {
    name: 'tom',
    lname: 'kurtz',
    link: '123'
  }, {
    name: 'newton',
    lname: 'king',
    link: '456'
  }, {
    name: 'jan',
    lname: 'heckal',
    link: '123'
  }]

for (i = 0, n = arr.length; i < n; i++) {
    var item = arr[i];
    arrResult[ item.name + " - " + item.lname ] = item; // create associative array
}

var i = 0;
var nonDuplicatedArray = [];    
for(var item in arrResult) {
    nonDuplicatedArray[i++] = arrResult[item]; // copy the objects that are now unique
}
console.log(nonDuplicatedArray)
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236