JSON对象属性-去重相加

方法一:
使用 findIndex方法

// 相加去重
    replace_plus(dataArr) { // dataArr 是数组
      var newData = []
      dataArr.forEach(function(el) { // forEach  循环出来是: index 和 每个index 对应的数据
        var result = newData.findIndex(function(ol) {  // el 和 ol 都是 [{},{},{}] 数据样式里的 {}
          return el.signCode=== ol.signCode
        })
        if (result !== -1) { // 存在重复 signCode 属性  其余的需要计算的属性  m01-m12  相加
          newData[result].m01 = Number(newData[result].m01) + Number(el.m01)
          newData[result].m02 = Number(newData[result].m02) + Number(el.m02)
          newData[result].m03 = Number(newData[result].m03) + Number(el.m03)
          newData[result].m04 = Number(newData[result].m04) + Number(el.m04)
          newData[result].m05 = Number(newData[result].m05) + Number(el.m05)
          newData[result].m06 = Number(newData[result].m06) + Number(el.m06)
          newData[result].m07 = Number(newData[result].m07) + Number(el.m07)
          newData[result].m08 = Number(newData[result].m08) + Number(el.m08)
          newData[result].m09 = Number(newData[result].m09) + Number(el.m09)
          newData[result].m10 = Number(newData[result].m10) + Number(el.m10)
          newData[result].m11 = Number(newData[result].m11) + Number(el.m11)
          newData[result].m12 = Number(newData[result].m12) + Number(el.m12)
        } else { // 不存在重复项
          el.m01 = Number(el.m01)
          el.m02 = Number(el.m02)
          el.m03 = Number(el.m03)
          el.m04 = Number(el.m04)
          el.m05 = Number(el.m05)
          el.m06 = Number(el.m06)
          el.m07 = Number(el.m07)
          el.m08 = Number(el.m08)
          el.m09 = Number(el.m09)
          el.m10 = Number(el.m10)
          el.m11 = Number(el.m11)
          el.m12 = Number(el.m12)
          newData.push(el)
        }
      })
      return newData
    },

调用方法:

this.allSummaryData = this.replace_plus(response.data.rows)

参考博文:https://hacpai.com/article/1536302199197

方法二:
由于 findIndex 在IE版本的不支持:
在这里插入图片描述
所以自己把 findIndex 方法拆开写了:

 // 相加去重
    replace_plus(dataArr) {
      var newData = []
      var result = -1
      for (let i = 0; i < dataArr.length; i++) {
        for (let k = 0; k < newData.length; k++) {
          if (dataArr[i].signCode=== newData[k].signCode) {
            result = k // 有相同signCode,跳出本次循环,返回newData的下标
            break
          } else {
            result = -1 // newData不存在当前项
            continue
          }
        }
        if (result !== -1) { // 有重复
          newData[result].m01 = Number(newData[result].m01) + Number(dataArr[i].m01)
          newData[result].m02 = Number(newData[result].m02) + Number(dataArr[i].m02)
          newData[result].m03 = Number(newData[result].m03) + Number(dataArr[i].m03)
          newData[result].m04 = Number(newData[result].m04) + Number(dataArr[i].m04)
          newData[result].m05 = Number(newData[result].m05) + Number(dataArr[i].m05)
          newData[result].m06 = Number(newData[result].m06) + Number(dataArr[i].m06)
          newData[result].m07 = Number(newData[result].m07) + Number(dataArr[i].m07)
          newData[result].m08 = Number(newData[result].m08) + Number(dataArr[i].m08)
          newData[result].m09 = Number(newData[result].m09) + Number(dataArr[i].m09)
          newData[result].m10 = Number(newData[result].m10) + Number(dataArr[i].m10)
          newData[result].m11 = Number(newData[result].m11) + Number(dataArr[i].m11)
          newData[result].m12 = Number(newData[result].m12) + Number(dataArr[i].m12)
        } else {
          dataArr[i].m01 = Number(dataArr[i].m01)
          dataArr[i].m02 = Number(dataArr[i].m02)
          dataArr[i].m03 = Number(dataArr[i].m03)
          dataArr[i].m04 = Number(dataArr[i].m04)
          dataArr[i].m05 = Number(dataArr[i].m05)
          dataArr[i].m06 = Number(dataArr[i].m06)
          dataArr[i].m07 = Number(dataArr[i].m07)
          dataArr[i].m08 = Number(dataArr[i].m08)
          dataArr[i].m09 = Number(dataArr[i].m09)
          dataArr[i].m10 = Number(dataArr[i].m10)
          dataArr[i].m11 = Number(dataArr[i].m11)
          dataArr[i].m12 = Number(dataArr[i].m12)
          newData.push(dataArr[i])
        }
      }
      return newData
    },

emmmmmm,虽然要处理的属性多了,代码感觉是个庞然大物,但亲测可用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值