var tmp = new Array()
for(var i = 0, j = jsArr.lengthi <ji++) {
var v = jsArr[i]
tmp[v] = (typeof(tmp[v]) != 'undefined') ? tmp[v] +1 : 1
}
这样就可以知道每个值出现的次数了
方法一:var ary = new Array("111","22","33","111")
var s = ary.join(",")+","
for(var i=0i<ary.lengthi++) {
if(s.replace(ary[i]+",","").indexOf(ary[i]+",")>-1) {
alert("数组中有重复元素:" + ary[i])
break外语屋
}
}
方法二:
var ary = new Array("111","22","33","111")
var nary=ary.sort()
for(var i=0i<ary.lengthi++){
if (nary[i]==nary[i+1]){
alert("数组重复内容:"+nary[i])
}
}
方法三:
function isRepeat(arr){
var hash = {}
for(var i in arr) {
if(hash[arr[i]])
return true
hash[arr[i]] = true
}
return false
}
//数组去重var arr = [1,3,5,6,1,4,2,6,7]
var newArr = []
for(var i in arr) {
if(newArr.indexOf(arr[i]) == -1) {
newArr.push(arr[i])
}
}
console.log(newArr)
判断是否有重复数据的话,只要判断arr和newArr是否相等就可以
//还有es6写法
var arr = [1,3,5,6,1,4,2,6,7]var newArr = Array.from(new Set(arr))
console.log(newArr)