I have a variable data a array of objects. Now I want to check if there a duplicates values except 0. What I've done so far is the code snippet below:
The alert shows me true it should be false cause 0 is not included for checking. Please help. Thanks
var data = [{id: 0}, {id: 1}, {id: 3}, {id: 0},];
var checkdata= data.map(function(item){
return item.id });
var isDuplicatedata= checkdata.some(function(item, idx){
return checkdata.indexOf(item) != idx
});
alert(isDuplicatedata)
You can use Array.prototype.some()
The
some()method tests whether some element in the array passes the test implemented by the provided function.
and a temporary object.
var data = [{ id: 0 }, { id: 1 }, { id: 3 }, { id: 0 } ],
object = {},
duplicate = data.some(function (a) {
if (a.id === 0) {
return false;
}
if (a.id in object) {
return true;
}
object[a.id] = true;
});
document.write(duplicate);
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