js去重 幾個實例。

貌似不太明白數組

<script> ide

Array.prototype.del = function() { this

var a = {}, c = [], l = this.length; .net

for (var i = 0; i < l; i++) { prototype

var b = this[i]; htm

var d = (typeof b) + b; ip

if (a[d] === undefined) { get

c.push(b); it

a[d] = 1; io

}

}

return c;

}

alert([1, 1, 2, 3, 4, 5, 4, 3, 4, 4, 5, 5, 6, 7].del());

</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;

}


詳細出處參考:http://www.jb51.net/article/25939.htm

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);


詳細出處參考:

http://www.jb51.net/article/25939.htm

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;

}


詳細出處參考:http://www.jb51.net/article/25939.htm

相關文章
相關標籤/搜索