This question already has an answer here:
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"
}
];
Another solution using
Array.prototype.reduce
and a hash table - see demo below:If you can use Javascript libraries such as underscore or lodash, I recommend having a look at _.uniq function in their libraries. From lodash:
Here you have to use like below,
Use Array.prototype.filter to filter out the elements and to keep a check of duplicates use a
temp
arrayYou could filter it with a hash table.
ES6 with
Set