zoukankan      html  css  js  c++  java
  • js 对象去重的方式

    一般在开发的过程中,都会碰到处理数据的数组去重或者对象去重,以下是去重的方式
    第一种方式: 
    // 对象根据条件去除重复数据
    RemoveDuplication(arr) {
    const hash = {}
    arr.reduce((obj, next) => {
    const hashId = `${next.date}_${next.userId}` // next.date和userId是去重的条件
    if (!hash[hashId]) {
    hash[`${next.date}_${next.userId}`] = true
    obj.push(next)
    }
    console.log(obj)
    return obj
    }, [])
    },
     
    再简化一下还可以写成
    const arr = this.tableCheckbox.reduce((item, next) => {
         obj[next.id] ? '' : obj[next.id] = true && item.push(next)
         return item
    }, [])
     
    第二种方式:
    es6里面的new Set方法  一般多用于数组去重
    也可以使用对象的去重方式
    new Set(newArrList)// 去重
     
    /*** 数组中对应数组对象中的值,不需要重新写一个变量直接就可以删除数据,只留下对应的数据   */
    const dateArr = [1,3,16,20]
    const schedual = [{name: '香蕉', id: 1}, {name: '梨', id: 6},{name: '苹果', id: 16}, {name: '橘子': 7}]
    // 根据dateArr中的数据,将 schedual里面的数据过滤
     
    schedual = schedual.filter(item => {
           return dateArr.indexOf(item.name) > -1 // 要检索的字符串值没有出现
    })
    consolelog(schedual) // [{name: '香蕉', id: 1},{name: '苹果', id: 16}]
  • 相关阅读:
    第四周
    第二次作业
    jsp
    软件测试第一次
    增删改查
    用户添加
    登录
    购物商城
    jsp第七周作业
    jsp第六周作业
  • 原文地址:https://www.cnblogs.com/vickyzhang/p/13753209.html
Copyright © 2011-2022 走看看