1.第一种方法
1 var arr = [1,2,3,1,2]
2 var newArr = []
3 for(var i = 0; i < arr.length; i++) {
4 var item = arr[i]
5 if(newArr.indexOf(item) === -1) {
6 newArr.push(item)
7 }
8 }
9 console.log(newArr) // [1,2,3]
2.第二种方法
1 var arr = [1,2,3,1,2]
2 var newArr = []
3 var obj = {}
4 for(var i = 0; i < arr.length; i++) {
5 var item = arr[i]
6 if(obj[item] === undefined) {
7 obj[item] = 1
8 newArr.push(item)
9 }
10 }
11 console.log(newArr) // [1,2,3]
转载于猿2048:⇛《数组去重》