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"
}
];
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With