一、set去重
var arr = [1,2,2,3,4,4,5,7,8,5];
console.log(arr);
var newArr = new Set(arr);
console.log(newArr);
二、对象去重
var arr = [1,2,2,3,4,4,5,7,8,5];
var obj = {};
var newArr = [];
arr.forEach(function(item,index) {
console.log(item);
obj[item] = "haha";
})
console.log(obj);
for (i in obj) {
newArr.push(i);
}
console.log(newArr);
三、for循环去重
var arr = [1, 2, 2, 3, 4, 4, 5, 7, 8, 5];
var newArr = [];
var flag = true;
for (var i = 0; i < arr.length; i++) {
flag = true;
for (var j = 0; j < newArr.length; j++) {
if(newArr[j] == arr[i]) {
flag = false;
}
}
if (flag) {
newArr.push(arr[i]);
}
}
console.log(newArr);