js的几种去重方法

1、include去重

仅限数组格式,对象格式不可以

 if (!this.cTypeList.includes(element["cTypestation"])) {
                    this.cTypeList.push(element["cTypestation"]);
                }
2、set去重 建立map表
public map = new Map<number, string>()
  //初始化查询 表格数据
  refresh() {
    this.source = [];    this.VegeStatisticsEleService.getNPPQuery().subscribe(data => {
      //区域代码cCode 去重
      let set = new Set<number>();
      data.forEach(d => {
        set.add(d.cCode);
      })
      //建立map表{ key:cCode value:cName}
      set.forEach(item => {
        this.promiseArr.push(this.AreaService.getByKey(item).then(nameData => {
          this.map.set(item, nameData.cName)
        }));
      })
      console.log(this.map);
    });
  }

//思路
li.isChecked=new Set(this.listOfId).has(li.id)

3.数组去重

e//年份
let item = {
      label: this.selYear.toString(),
      pngPrnStr: this.productService.makeProductName("BEHB", "VEGE", this.selYear + this.dateArr[1] + "00000000", this.selYear + this.dateArr[0] + "00000000", this.field, this.reso, "HL", "png"),
      imgPrnStr: this.productService.makeProductName("BEHB", "VEGE", this.selYear + this.dateArr[1] + "00000000", this.selYear + this.dateArr[0] + "00000000", this.field, this.reso, "HL", "img")
    }

    this.array.forEach((d, i) => {
        if (d.label == e) {
           this.array.splice(i, 1, item)//有相同年份数据,更新该键值
        }
      })
    if (!this.array.includes(item)) {
      this.array.push(item);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值