数组合并去重和数组对象合并去重

一、数组合并去重

  例:let arr1 = [1, 2,3, 4, 5, 7]

    let arr2 = [1, 3, 5, 7,8, 10]

    let arr = arr1.concat(arr2)   -> 合并数组

    let newArr = [...new Set(arr)]      ------   或者:let newArr = new Set(arr)  Array.form(newArr)

    console.log(newArr)   //  [1, 2, 3, 4, 5, 7, 8, 10]

二、数组对象合并去重

  例: let arr1 = [{'id': '111', name: 'xc'}, {'id': '222', name: 'bb'}]
     let arr2 = [{'id': '111', name: 'xc'}, {'id': '333', name: 'hh'}]
     let newArr = arr1.concat(arr2)
     let list = []

    for (let item1 of newArr) {

      let flag = true

      for (let item2 of list) {

        if (item1.id == item2.id) {

          flag = false

        }

      }

      if (flag) { list.push(item1) }

    }
    console.log(list)    //[{'id': '111', name: 'xc'}, {'id': '222', name: 'bb'}, {'id': '333', name: 'hh'}]

原文地址:https://www.cnblogs.com/qianxiaoniantianxin/p/14384494.html