JS判断数组中是否有重复值得三种实用方法_javascript技巧

WBOY
Release: 2016-05-16 17:25:26
Original
1353 people have browsed it

方法一:

复制代码代码如下:

var ary = new Array("111","22","33","111");

var s = ary.join(",")+",";

for(var i=0;i
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=0;i
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;

}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn