I have following code for remove duplicate record. It shows me unique values and removes the last duplicate record. However, what I want is, if two passports are the same, remove both elements from the array.
Example
var array = [{
"PassportNo": "abced",
"Name": "John"
},
{
"PassportNo": "abcederrr",
"Name": "Johnss",
},
{
"PassportNo": "abced",
"Name": "John"
}
];
function removeDuplicates(originalArray, objKey) {
var trimmedArray = [];
var values = [];
var value;
for (var i = 0; i < originalArray.length; i++) {
value = originalArray[i][objKey];
if (values.indexOf(value) === -1) {
trimmedArray.push(originalArray[i]);
values.push(value);
}
}
return trimmedArray;
}
var noDuplicates = removeDuplicates(array, 'PassportNo');
console.log(noDuplicates);
/*
[
{
"PassportNo": "abced",
"Name": "John"
},
{
"PassportNo": "abcederrr",
"Name": "Johnss"
}
]
*/
I want like this (remove both values):
[{
"PassportNo": "abcederrr",
"Name": "Johnss"
}]
var removedublicate ==is comparation, just use one '=' for assignation. Can you also edit it with your result please?