• 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",
          },
        ];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    结果数据

        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",
          },
        ];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    实现方法

    	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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    码农的灵魂拷问之(二)什么是应用软件
    MySQL-数据库事务详解
    什么是大模型微调?微调的分类、方法、和步骤
    internship:svn的使用
    使用OpenCV的函数hconcat()、vconcat()实现图像或矩阵的连接
    Navicat 16 下载、安装、重装
    为什么用二进制进行数据传输、二进制概述及移位运算和乘除的关系
    ES6面试题总结
    【Java】Object 类
    Javascript知识【JSON】
  • 原文地址:https://blog.csdn.net/qq_38157825/article/details/127668988