JS判断一个数组中是否有重复值的三种方法
1个回答
展开全部
JS判断一个数组中是否有重复值的三种方法方法一:
var ary = new Array("111","22","33","111");
var s = ary.join(",")+",";
for(var i=0;i<ary.length;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<ary.length;i++){
if (nary[i]==nary[i+1]){
alert("数组重复内容:"+nary[i]);}}方法三:
function isRepeat(arr){
var hash = {};
var ary = new Array("111","22","33","111");
var s = ary.join(",")+",";
for(var i=0;i<ary.length;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<ary.length;i++){
if (nary[i]==nary[i+1]){
alert("数组重复内容:"+nary[i]);}}方法三:
function isRepeat(arr){
var hash = {};
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询