js去除数组中的重复值,有多种方法,本文总结了四种方法供大家参考。
1,js数组中去除重复值的方法一。
<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>
2,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; }
3,js数组中去除重复值的方法三
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);
4,js数组中去除重复值的方法四
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; }
js数组中去除重复值的四种方法,各有特色,大家看看哪种更方便好用,择取而用之吧。