本文章提供了四種刪除重複陣列元素的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>