js 刪除陣列中重複項的幾種方法

來源:互聯網
上載者:User
關鍵字 網頁製作 Ajax JavaScript

本文章提供了四種刪除重複陣列元素的js代碼,四種函數最簡單的確刪除重複陣列元素的方法是正則運算式了,下面來看看那款更適合你吧

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "HTTP://www.w3.org/tr/xhtml1/dtd/ xhtml1-transitional.dtd">
<html xmlns="HTTP://www.jzread.com/1999/xhtml">
<head>
< meta HTTP-equiv="content-type" content="text/html; charset=gb2312" />
<title>js 刪除陣列中重複項的幾種方法</title>
<script>
function unique(data){
 data = data || [];
 var a = {};
 len = data.length;
 for (var i=0; i<len;i++){
  var v = data[i];
  if (typeof(a[v]) == 'undefined'){
   a[v] = 1;
  }
 };
 data.length=0;
 for (var i in a){
  data[data.length] = i;
 }
 return data;
}
function test(){
 var arr = [9,1,3,8,7,7,6,6,5,7,8,8,7,4,3,1];
 var arr1 = unique(arr);
 alert(arr1.join(","));
}
test();
</script>
//方法二

<script language="網頁特效">
function getnorepeat() {
return arguments[0].join('‖').match(/(b[^‖]+b)(?!. *‖1b)/ig);
}
var tmparr = [1, 'a', 'ab', 'abc', 'd', 1.2, 'a+b', 'd', 'e', 5, 'www.jzread.com', 1, 'h', 'c', 'ab'];
var retarr = getnorepeat(tmparr);
alert(retarr);
</script>
方法三

<script>
var s = "0,1,2,5,3,2,12,4,1,1,123450";
var stmp = (","+s.split(",").reverse().join(",")+",").replace(/,([^,]+)(?=,.*,1,)/ig, '').split(",").reverse(). join();
stmp = stmp.substr(1, stmp.length-2);
alert(stmp)
</script> </head>

<body>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.