一级数组

  1. 方法一 使用 es6 的 Set 和 Array.from 方法

    let arr1 = [1, 2, 3, 4, 5, 6, 2, 3]
    let arr2 = [1, 2, 3, 4, 5, 6, 5, 6]
    let result = Array.from(new Set([...arr1, ...arr2]))
    console.log(result) // [1, 2, 3, 4, 5, 6]
  2. 方法二 使用 indexOf 方法

    let arr1 = [1, 2, 3, 4, 5, 6, 2, 3]
    let arr2 = [1, 2, 3, 4, 5, 6, 5, 6]
    let arr3 = arr1.concat(arr2)
    function uniqueFun(arr) {
     const newArr = []
     for (let i = 0; i < arr.length; i++) {
         if (newArr.indexOf(arr[i]) == -1) {
             newArr.push(arr[i])
         }
     }
     return newArr
    }
    console.log(uniqueFun(arr3)) // [1, 2, 3, 4, 5, 6]
  3. 方法三 使用 includes 方法

    let arr1 = [1, 2, 3, 4, 5, 6, 2, 3]
    let arr2 = [1, 2, 3, 4, 5, 6, 5, 6]
    let arr3 = arr1.concat(arr2)
    function uniqueFun(arr) {
     const newArr = []
     for (let i = 0; i < arr.length; i++) {
         if (!newArr.includes(arr[i])) {
             newArr.push(arr[i])
         }
     }
     return newArr
    }
    console.log(uniqueFun(arr3)) // [1, 2, 3, 4, 5, 6]
    
  4. 方法四 使用 filter 方法

    let arr1 = [1, 2, 3, 4, 5, 6, 2, 3]
    let arr2 = [1, 2, 3, 4, 5, 6, 5, 6]
    let arr3 = arr1.concat(arr2) // [1, 2, 3, 4, 5, 6, 2, 3, 1, 2, 3, 4, 5, 6, 5, 6]
    function uniqueFun(arr) {
     return arr.filter((item, index, arr) => {
         return arr.indexOf(item) == index // 返回当前元素在原始数组的索引值 == 当前索引值的元素
     })
    }
    conosle.log(uniqueFun(arr3)) // [1, 2, 3, 4, 5, 6]
  5. 方法五 使用 es6 Map

    let arr1 = [1, 2, 3, 4, 5, 6, 2, 3]
    let arr2 = [1, 2, 3, 4, 5, 6, 5, 6]
    let arr3 = arr1.concat(arr2) 
    function uniqueFun(arr) {
     let map = new Map()
     return arr.filter(item => {
         return !map.has(item) && map.set(item, 1)
     })
    }
    console.log(uniqueFun(arr3)) // [1, 2, 3, 4, 5, 6]

数组对象

  1. 方法一 使用扩展运算符(...), filter 和 Map

    let arr1 = [{id: 1, name: '小明'}, {id: 2, name: '小红'}, {id: 4, name: '小刘'}]
    let arr2 = [{id: 1, name: '小明'}, {id: 2, name: '小红'}, {id: 3, name: '小王'}]
    let arr3 = [...arr1, ...arr2]
    function uniqueFun(arr) {
     const map = new Map()
     return arr.filter(item => {
         return !map.has(item.id) && map.set(item.id, 1)
     })
    }
    console.log(uniqueFun(arr3)) // 输出 [{id: 1, name: '小明}, {id: 2, name: '小红'}, {id: 4, name: '小刘'}, {id: 3, name: '小王'}]
  2. 方法二 使用扩展运算符(...), filter 和对象字面量 {}

    let arr1 = [{id: 1, name: '小明'}, {id: 2, name: '小红'}, {id: 4, name: '小刘'}]
    let arr2 = [{id: 1, name: '小明'}, {id: 2, name: '小红'}, {id: 3, name: '小王'}]
    let arr3 = [...arr1, ...arr2]
    function uniqueFun(arr) {
     const obj = {}
     return arr.filter(item => {
         return !obj.hasOwnProperty(item.id) && (obj[item.id] = 1)
     })
    }
    console.log(uniqueFun(arr3)) // 输出 [{id: 1, name: '小明}, {id: 2, name: '小红'}, {id: 4, name: '小刘'}, {id: 3, name: '小王'}]
  3. 方法三 使用concat, reduce 和 find

    let arr1 = [{id: 1, name: '小明'}, {id: 2, name: '小红'}, {id: 4, name: '小刘'}]
    let arr2 = [{id: 1, name: '小明'}, {id: 2, name: '小红'}, {id: 3, name: '小王'}]
    let arr3 = arr1.concat(arr2)
    function uniqueFun(arr) {
     return arr.reduce((pre, cur) => {
         if (!pre.find(item => item.id === cur.id)) {
             pre.push(cur)
         }
         return pre
     }, [])
    }
    console.log(uniqueFun(arr3)) // 输出 [{id: 1, name: '小明}, {id: 2, name: '小红'}, {id: 4, name: '小刘'}, {id: 3, name: '小王'}]

清儿
72 声望0 粉丝