1.ES6 set方法
let arr = [12,43,23,43,68,12]; let item = [...new Set(arr)]; console.log(item);//[12, 43, 23, 68]
let arr = [12,43,23,43,68,12]; let item = new Set(arr); console.log(item);//结果输出的是一个对象 //使用Array.from转成数组 let arr = [12,43,23,43,68,12]; let item = Array.from(new Set(arr)); console.log(item);// [12, 43, 23, 68]
2.hash方法
function unique3(arr){ for (var i =0,hash=[];i<arr.length ;i++ ){ if (hash[arr[i]]==undefined){ hash[arr[i]]=1; } } var j=0; var keys=[]; for (keys[i++] in hash ); return keys; } console.log( unique3(arr) );
原文:https://www.cnblogs.com/apple78/p/13027496.html