js去除数组重复项,删除重复数据的方法
- var arr = [3,3,1,6,7,4,4,1];
- var o = {};
- for(var i = 0; i < arr.length; i++)
- {
- o[arr[i]] = arr[i];
- }
- arr = [];
- for (var el in o)
- {
- if (el == o[el])
- arr.push(el - 0);
- }
- alert(arr);
- var clearRepeat=function(a){
- var c=[],b={};
- for(var i=0;i<a.length;i++){
- if( ! b[0+a[i]]){
- b[a[i]]=1;
- c.push(a[i])
- }
- }
- return c;
- }
- var a=[undefined,4,5,6,7,8,9,10,11,"aa","bb","aa","bb","$#@@","!*&^%%","$#@@","!*&^%%",0,"0"];
- //请注意上面数组的0,"0"项目。是可以正常处理的。因为他们的数据类型不一样所以他不能算是重复项
- var c=clearRepeat(a);
- alert(c.join())
- Array.prototype.del = function() {
- var a = {}, c = [], l = this.length;
- for (var i = 0; i < l; i++) {
- var b = this[i];
- var d = (typeof b) + b;
- if (a[d] === undefined) {
- c.push(b);
- a[d] = 1;
- }
- }
- return c;
- }
- alert([1, 1, 2, 3, 4, 5, 4, 3, 4, 4, 5, 5, 6, 7].del());
- Array.prototype.unique = function(){
- var o = {}, re = [];
- for(var i=0, len = this.length;i<len; i++){
- if(!o[this[i]]){
- o[this[i]] = true;
- re.push(this[i]);
- }
- }
- return re;
- }
- var arr = [1,2,3,2,3,4,1,2,3,1,1,1,5,6];
- console.log( arr.unique() );
- 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);