如何检查数组中,除了重复的javascript 0 [英] How to check duplicates in array except 0 javascript
本文介绍了如何检查数组中,除了重复的javascript 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量数据
对象的数组。现在我要检查是否有除0。重复值是什么我迄今所做低于code片断:
警报显示我真应该是假性因果0不包括检查。请帮忙。谢谢
\r
\r\r
\r VAR数据= {[ID:0},{ID:1},{ ID:3},{ID:0}]。\r
\r
\r
VAR checkdata = Data.Map中(函数(项目){\r
返回item.id});\r
VAR isDuplicatedata = checkdata.some(函数(项目,IDX){\r
返回checkdata.indexOf(项目)!= IDX\r
});\r
\r
警报(isDuplicatedata)\r
\r
解决方案
对象无法相比其他基本型相比的方式。
我写了一个函数来解决你的要求,其从你实现什么完全不同和在性能和实用性方面,它可能是不好
VAR数据= {[ID:0},{ID:1},{ID:2},{ID:0},{ID:3}];功能isDuplicatedata(){
对于(VAR I = 0; I< data.length;我++){
如果(数据[I] .ID === 0)
继续;
对于(VAR J = I + 1; J< data.length; J ++){
如果(数据[J] .ID ===数据[I] .ID)
返回true;
}
}
返回false;
}警报(isDuplicatedata())
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)
解决方案
Object cannot be compared the way other primitive type compared.
i wrote a function to solve what you asked, its completely different from what you implemented and it may be not good in terms of performance and practicality
var data = [{id: 0}, {id: 1}, {id: 2}, {id: 0},{id: 3}];
function isDuplicatedata() {
for(var i = 0; i < data.length; i++) {
if(data[i].id === 0)
continue;
for(var j = i+1; j < data.length; j++) {
if(data[j].id === data[i].id)
return true;
}
}
return false;
}
alert(isDuplicatedata())
这篇关于如何检查数组中,除了重复的javascript 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文