<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js數組去重</title> </head> <body> <script type="text/javascript"> var arr = new Array(1,2,3,4,5,3,4,3,4,7,8); list = unique(arr); alert('list'+list); console.log(list); function unique(arr){ list = [arr[0]]; arr.sort(); for(var i=1;i<arr.length;i++){ if(arr[i] != arr[i-1] ){ list.push(arr[i]) } } return list; } function unique1(arr){ var list = []; for(var i=0;i<arr.length;i++){ if(arr.indexOf(arr[i]) == i){ list.push(arr[i]); } } return list; } function unique2(arr){ var hash=[]; for(var i=0;i<arr.length;i++){ for(var j=i+1;j<arr.length;j++){ if(arr[i] == arr[j]){ i++; } } hash.push(arr[i]); } return hash; } function unique3(arr){ var x = new Set(arr); return [...x]; } </script> </body> </html>
轉載:https://www.cnblogs.com/jiayuexuan/p/7527055.htmljavascript