标签:length dex 数组排序 字符 console color inf turn png
function isRepeat() {
var ary = new Array("111", "22","22", "33", "111");
for (var i = 0; i < ary.length; i++) {
console.log(ary.indexOf(ary[i]));
if(ary.indexOf(ary[i]) != i) {
alert("数组中有重复元素:" + ary[i])
}
}
}
indexOf方法返回元素在字符串中首次出现的位置,如果有重复元素,必定会出现多次相同的位置。如下:
function isRepeat() {
var ary = new Array("111", "22","22", "33", "111");
var newary = ary.sort()
for(var i = 0; i< newary.length; i++) {
if(newary[i] == newary[i+1]) {
alert("数组重复内容:" + newary[i]);
}
}
}
function isRepeat(arr) {
var hash = {};
for (var i in arr) {
if (hash[arr[i]]){
return true;
}
hash[arr[i]] = true;
}
return false;
}
标签:length dex 数组排序 字符 console color inf turn png
原文地址:https://www.cnblogs.com/lyt0207/p/12354327.html