7

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

Example:

var newarray=[
    {
        Email:"[email protected]",
        ID:"A"
    },
    {
        Email:"[email protected]",
        ID:"B"
    },
    {
        Email:"[email protected]",
        ID:"A"
    },
    {
        Email:"[email protected]",
        ID:"C"
    },
    {
        Email:"[email protected]",
        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:"[email protected]",
        ID:"A"
    },
    {
        Email:"[email protected]",
        ID:"B"
    },  
    {
        Email:"[email protected]",
        ID:"C"
    }
];
Santosh Khavekar
  • 597
  • 1
  • 6
  • 22

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: "[email protected]",
  ID: "A"
}, {
  Email: "[email protected]",
  ID: "B"
}, {
  Email: "[email protected]",
  ID: "A"
}, {
  Email: "[email protected]",
  ID: "C"
}, {
  Email: "[email protected]",
  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: "[email protected]", ID: "A" }, { Email: "[email protected]", ID: "B" }, { Email: "[email protected]", ID: "A" }, { Email: "[email protected]", ID: "C" }, { Email: "[email protected]", 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: "[email protected]", ID: "A" }, { Email: "[email protected]", ID: "B" }, { Email: "[email protected]", ID: "A" }, { Email: "[email protected]", ID: "C" }, { Email: "[email protected]", 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:"[email protected]", ID:"A" }, { Email:"[email protected]", ID:"B" }, { Email:"[email protected]", ID:"A" }, { Email:"[email protected]", ID:"C" }, { Email:"[email protected]", 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