1、第一种方法 SE6 Set 去重
var arr = [1,2,3,4,5,6,1,3,4]; var arr1 = new Set(arr); console.log(arr1);
2、第二种方法:利用indexOf去重
<script>
function aa(array){ var arr = []; for(var i = 0; i < array.length; i++){ if(arr.indexOf(array[i]) == -1){ arr.push(array[i]); } } return arr; } var bb = [1,1,1,1,2,2,2,3,3,3,4,4,4]; console.log(aa(bb)); </script>
3、第三种:利用for嵌套for,然后splice去重
function unique(arr){ for(var i=0; i<arr.length; i++){ for(var j=i+1; j<arr.length; j++){ if(arr[i]==arr[j]){ //第一个等同于第二个,splice方法删除第二个 arr.splice(j,1); j--; } } } return arr; }