合并两个数组并去重(ES5和ES6两种方式实现)
ES6实现方式
let arr1 = [1, 1, 2, 3, 6, 9, 5, 5, 4]
let arr2 = [1, 2, 5, 4, 9, 7, 7, 8, 8]
function uniqueArr(arr1,arr2) {
//合并两个数组
arr1.push(...arr2)//或者arr1 = [...arr1,...arr2]
//去重
let arr3 = Array.from(new Set(arr1)) //let arr3 = [...new Set(arr1)]
console.log(arr3)
}
uniqueArr(arr1,arr2)
ES5实现方式
var arr1 = [1, 1, 2, 3, 6, 9, 5, 5, 4]
var arr2 = [1, 2, 5, 4, 9, 7, 7, 8, 8]
function uniqueArr(arr1, arr2){
var arr3 = arr1.concat(arr2)
var arr4 = []
for(var i=0,len=arr3.length; i<len; i++) {
if(arr4.indexOf(arr3[i]) === -1) {
arr4.push(arr3[i])
}
}
console.log(arr4)
}
uniqueArr(arr1, arr2)