js如何判断一个数组中是否有重复的值

引自:http://bbs.tianya.cn/post-414-38497-1.shtml

 

方法一:

  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("有重复!");
  }
  }

  方法二:

  var ary = new Array("111","22","33","111");
  var nary=ary.sort();
  for(var i=0;i<nary.length-1;i++)
  {if (nary[i]==nary[i+1])
  {alert("重复内容:"+nary[i]);}
  }

  方法三:

  var hash = {};
  for(var i in arr) {
  if(hash[arr[i]])
  return true;
  hash[arr[i]] = true;
  }
  return false;

posted @ 2014-10-11 17:32  doubledu  阅读(555)  评论(0编辑  收藏  举报