js数组对象中根据某个相同的字段找到其他对象中有值的数据

初始数据:根据 batch_code 字段的值相同,找到其他有值的字段,组成只有一条该batch_code的值,其他项完整的有值数据

    let initData = [
      {
        yshc_clwd: "80.8712386415",
        ysgz_clhsl: "",
        cpjx_fwt: "50.0000000000",
        ysd: "",
        sshc_wt: "",
        ysgz_fwt: "50.0000000000",
        batch_score: "",
        batch_code: "y221029001650282001",
      },
      {
        yshc_clwd: "",
        ysgz_clhsl: "90.6153316521",
        cpjx_fwt: "",
        ysd: "77.0842467824",
        sshc_wt: "",
        ysgz_fwt: "",
        batch_score: "76.61074544869737",
        batch_code: "y221029001650282001",
      },
      {
        yshc_clwd: "",
        ysgz_clhsl: "",
        cpjx_fwt: "",
        ysd: "",
        sshc_wt: "71.7425238034",
        ysgz_fwt: "",
        batch_score: "",
        batch_code: "y221029001650282001",
      },
      {
        yshc_clwd: "78.2137372538",
        ysgz_clhsl: "91.3525032524",
        cpjx_fwt: "100.0000000000",
        ysd: "93.0951578045",
        sshc_wt: "56.6886235075",
        ysgz_fwt: "100.0000000000",
        batch_score: "92.69054921798096",
        batch_code: "221029002320382001",
      },
      {
        yshc_clwd: "79.6749437612",
        ysgz_clhsl: "89.7717254632",
        cpjx_fwt: "0.0000000000",
        ysd: "62.8911063313",
        sshc_wt: "64.4381194367",
        ysgz_fwt: "0.0000000000",
        batch_score: "59.711997954431254",
        batch_code: "221030003910182001",
      },
      {
        yshc_clwd: "79.2453956916",
        ysgz_clhsl: "88.8328338420",
        cpjx_fwt: "0.0000000000",
        ysd: "79.2817805662",
        sshc_wt: "66.7184293012",
        ysgz_fwt: "50.0000000000",
        batch_score: "66.56589084518512",
        batch_code: "221030003910282001",
      },
    ];

结果数据

    let resData = [
      {
        yshc_clwd: "80.8712386415",
        ysgz_clhsl: "90.6153316521",
        cpjx_fwt: "50.0000000000",
        ysd: "77.0842467824",
        sshc_wt: "71.7425238034",
        ysgz_fwt: "50.0000000000",
        batch_score: "76.61074544869737",
        batch_code: "y221029001650282001",
      },
      {
        yshc_clwd: "78.2137372538",
        ysgz_clhsl: "91.3525032524",
        cpjx_fwt: "100.0000000000",
        ysd: "93.0951578045",
        sshc_wt: "56.6886235075",
        ysgz_fwt: "100.0000000000",
        batch_score: "92.69054921798096",
        batch_code: "221029002320382001",
      },
      {
        yshc_clwd: "79.6749437612",
        ysgz_clhsl: "89.7717254632",
        cpjx_fwt: "0.0000000000",
        ysd: "62.8911063313",
        sshc_wt: "64.4381194367",
        ysgz_fwt: "0.0000000000",
        batch_score: "59.711997954431254",
        batch_code: "221030003910182001",
      },
      {
        yshc_clwd: "79.2453956916",
        ysgz_clhsl: "88.8328338420",
        cpjx_fwt: "0.0000000000",
        ysd: "79.2817805662",
        sshc_wt: "66.7184293012",
        ysgz_fwt: "50.0000000000",
        batch_score: "66.56589084518512",
        batch_code: "221030003910282001",
      },
    ];

实现方法

	arrayTransfer(data) {
      const resultArr = [];
      data.forEach(function (el) {
        let obj = {
          yshc_clwd: el.yshc_clwd,
          ysgz_clhsl: el.ysgz_clhsl,
          cpjx_fwt: el.cpjx_fwt,
          ysd: el.ysd,
          sshc_wt: el.sshc_wt,
          ysgz_fwt: el.ysgz_fwt,
          batch_score: el.batch_score,
          batch_code: el.batch_code,
        };
        for (let i = 0; i < resultArr.length; i++) {
          if (resultArr[i].batch_code === el.batch_code) {
            Object.keys(obj)?.forEach((the) => {
              if (!resultArr[i][the]) {
                resultArr[i][the] = el[the];
              }
            });
            return;
          }
        }
        resultArr.push(obj);
      });
      return resultArr;
    },
    resData = arrayTransfer(initData)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值