二维数组去重
作者:互联网
方法一:
1 handleArr () { 2 let arr = [[1], [1], [1, 2], [3], [1], [2, 4, 6], [3]] 3 let obj = {} 4 arr.forEach(item => { 5 obj[item] = item 6 }) 7 console.log(Object.values(obj)) 8 }
方法二:
1 handleArr () { 2 let arr = [[1], [1], [1, 2], [3], [1], [2, 4, 6], [3]] 3 let hash = {} 4 let result = [] 5 arr.forEach(item => { 6 if (!hash[item]) { 7 result.push(item) 8 hash[item] = true 9 } 10 }) 11 console.log(result) 12 }
方法三:
1 handleArr () { 2 let arr = [[1], [1], [1, 2], [3], [1], [2, 4, 6], [3]] 3 let result = [] 4 for (var i = 0; i < arr.length; i++) { 5 for (var j = 0; j < arr[i].length; j++) { 6 var v = this.arr[i][j] 7 if (!result.includes(v)) { 8 result.push(v) 9 } 10 } 11 } 12 console.log(result) 13 }
方法四:
1 handleArr (arr) { 2 let obj = {} 3 return arr.filter(item => { 4 if (!obj[item.toString()]) { 5 obj[item.toString()] = item.toString() 6 return item 7 } 8 }) 9 }
1 let arr = [[1], [1], [1, 2], [3], [1], [2, 4, 6], [3]] 2 console.log(this.handleArr(arr))
标签:arr,obj,handleArr,item,二维,let,result,数组 来源: https://www.cnblogs.com/guanhx/p/16620560.html