DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js數組中刪除重復值的代碼小結
js數組中刪除重復值的代碼小結
編輯:關於JavaScript     
js數組中去除重復值

[Ctrl+A 全選 注:如需引入外部Js需刷新才能執行]
方法二
復制代碼 代碼如下:
//去重復數組
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;
}

方法三
復制代碼 代碼如下:
var arr = ["123","123","123","123","sfsdf","123","345","123","123","345","456","567","sdc"];
var str = [];
for(var i = 0,len = arr.length;i < len;i++){
! RegExp(arr[i],"g").test(str.join(",")) && (str.push(arr[i]));
}
alert(str);

方法四
復制代碼 代碼如下:
var pureMulti1=function(arr){
var obj={};
var a = [];
for(var i=0,l=arr.length;iif(!((arr[i]+"") in obj)){
a.push(arr[i]);
}
obj[arr[i]]="";
}
return a;
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved