初始数据:根据 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)