7

I have array with obejcts email and Id so I want delete duplicate elements who have similar ID's.

Example:

var newarray=[
    {
        Email:"test1@gmail.com",
        ID:"A"
    },
    {
        Email:"test2@gmail.com",
        ID:"B"
    },
    {
        Email:"test3@gmail.com",
        ID:"A"
    },
    {
        Email:"test4@gmail.com",
        ID:"C"
    },
    {
        Email:"test4@gmail.com",
        ID:"C"
    }
];

Now I need to delete Duplicate elements which have ID's are common.In the sence I am expecting final Array is

var FinalArray=[
    {
        Email:"test1@gmail.com",
        ID:"A"
    },
    {
        Email:"test2@gmail.com",
        ID:"B"
    },  
    {
        Email:"test5@gmail.com",
        ID:"C"
    }
];
Santosh Khavekar
  • 597
  • 1
  • 6
  • 22
  • All you need is to simply create a new array, iterate through your original array and fill new array with those items which are not duplicate. Of course, you may want to improve this performance by using object or Map or whatever else, but idea is the same. – Yeldar Kurmangaliyev Nov 08 '16 at 08:52
  • You expect `test1` for `ID:"A"` but `test5` for `ID:"C"`. Does it mean you don't care which duplicate is filtered out? – pawel Nov 08 '16 at 08:54
  • why `'test5@gmail.com'` and not `'test4@gmail.com'`? – Nina Scholz Nov 08 '16 at 08:55
  • Thanks to All for your so quick answers. – Santosh Khavekar Nov 08 '16 at 08:56
  • Why marked as duplicate???? Can any one find any codes from the linked question that solve this question?? – tsh Mar 02 '18 at 04:14
  • Here is one good way to achieve this: https://stackoverflow.com/questions/15125920/how-to-get-distinct-values-from-an-array-of-objects-in-javascript/58429784#58429784 – Arun Saini Jul 24 '20 at 18:28

4 Answers4

6

Use Array.prototype.filter to filter out the elements and to keep a check of duplicates use a temp array

var newarray = [{
  Email: "test1@gmail.com",
  ID: "A"
}, {
  Email: "test2@gmail.com",
  ID: "B"
}, {
  Email: "test3@gmail.com",
  ID: "A"
}, {
  Email: "test4@gmail.com",
  ID: "C"
}, {
  Email: "test5@gmail.com",
  ID: "C"
}];
   
// Array to keep track of duplicates
var dups = [];
var arr = newarray.filter(function(el) {
  // If it is not a duplicate, return true
  if (dups.indexOf(el.ID) == -1) {
    dups.push(el.ID);
    return true;
  }

  return false;
  
});

console.log(arr);
void
  • 36,090
  • 8
  • 62
  • 107
4

You could filter it with a hash table.

var newarray = [{ Email: "test1@gmail.com", ID: "A" }, { Email: "test2@gmail.com", ID: "B" }, { Email: "test3@gmail.com", ID: "A" }, { Email: "test4@gmail.com", ID: "C" }, { Email: "test5@gmail.com", ID: "C" }],
    filtered = newarray.filter(function (a) {
        if (!this[a.ID]) {
            this[a.ID] = true;
            return true;
        }
    }, Object.create(null));

console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6 with Set

var newarray = [{ Email: "test1@gmail.com", ID: "A" }, { Email: "test2@gmail.com", ID: "B" }, { Email: "test3@gmail.com", ID: "A" }, { Email: "test4@gmail.com", ID: "C" }, { Email: "test5@gmail.com", ID: "C" }],
    filtered = newarray.filter((s => a => !s.has(a.ID) && s.add(a.ID))(new Set));

console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

If you can use Javascript libraries such as underscore or lodash, I recommend having a look at _.uniq function in their libraries. From lodash:

_.uniq(array, [isSorted=false], [callback=_.identity], [thisArg])

Here you have to use like below,

var non_duplidated_data = _.uniq(newarray, 'ID'); 
mymotherland
  • 7,968
  • 14
  • 65
  • 122
1

Another solution using Array.prototype.reduce and a hash table - see demo below:

var newarray=[ { Email:"test1@gmail.com", ID:"A" }, { Email:"test2@gmail.com", ID:"B" }, { Email:"test3@gmail.com", ID:"A" }, { Email:"test4@gmail.com", ID:"C" }, { Email:"test5@gmail.com", ID:"C" } ];

var result = newarray.reduce(function(hash){
  return function(prev,curr){
     !hash[curr.ID] && (hash[curr.ID]=prev.push(curr));
     return prev;
  };
}(Object.create(null)),[]);

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}
kukkuz
  • 41,512
  • 6
  • 59
  • 95