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


    一、数组合并去重

      例: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'}]

  • 相关阅读:
    js禁用回退键
    css和js引用图片路径
    js 文字横向滚动
    数组转换
    Vbox共享串口
    office2003 打开docx文件
    注销退出客户点击回退怎么办
    vs2010 安装mvc3
    修改头像
    iis发布网站局域网无法访问
  • 原文地址:https://www.cnblogs.com/qianxiaoniantianxin/p/14384494.html
Copyright © 2020-2023  润新知