方法一:
function uniq(array){ // 定义一个空的数组 var temp = [] // 如果当前数组的第i项在当前数组中第一次出现的位置是i,才存到数组。否则代表是重复的 for(var i = 0; i<array.length;i++){ if(array.indexOf(array[i])==i){ temp.push(array[i]) } } return temp; } const result1 = uniq([1,5,3,6,2,1,3,7,0,5]) console.log(result1)方法二:利用ES6 Set容器
const uniqArr = arr => [...new Set(arr)] const result2 = uniqArr([1,5,3,6,2,1,3,7,0,5]) console.log(result2)