方法一:
function isDuplicate(arr) {
var temp = [] //临时数组
for (var i = 0; i < arr.length; i++) {
if (temp.indexOf(arr[i]) == -1) {
temp.push(arr[i])
}
}
return temp
};
var array = [12, 87, 45, 12, 9, 8, 3, 9];
console.log(isDuplicate(array)); //[12, 87, 45, 9, 8, 3]
方法二
function unique4(arr) {
if (!Array.isArray(arr)) {
return "not an array";
}
return Array.from(new Set(arr));
}
方法三
let array = [1,1,1,1,2,3,4,4,5,]
let set = new Set(array);
console.log(set);