方法1:
使用 Array.from() + set 数据结构
let arr = [1, 3, 3, 5];
// 方法1
// Array.from(): 将其他对象转化为数组
// set数据结构:ES6,类似于数组,但其成员值都是唯一的
let method1 = array => Array.from(new Set(array));
console.log(method1(arr)); // [1,3,5]
方法2
const method2= arr => [...new Set(arr)];
console.log(method2(arr)); // [1,3,5]
方法3
let method3 = array => {
let newArr = [];
for(let i = 0; i < array.length; i++){
if(!newArr.includes(array[i])){
newArr.push(array[i]);
}
}
return newArr;
}
console.log(method3(arr)); // [1,3,5]