Home  >  Article  >  Web Front-end  >  Code summary for deleting duplicate values ​​in js array_javascript skills

Code summary for deleting duplicate values ​​in js array_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:12:29847browse

Remove duplicate values ​​from js array


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Method 2 The code is as follows:


//Deduplicate array
function unique(data){
data = data || [];
var a = {};
len = data.length;
for (var i=0; ivar 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;
}


Method 3 The code is as follows:


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


Method 4 The code is as follows:


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;
}
<script> 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()); </script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn