业务场景:
需求:
reasons 那层数据可以删除,同时 measures 那层数据也可是删除。
实现:
reasons 那层删除很容易想到,就根据 reason 的 id 遍历匹配。利用数组的 filter 方法:
let reasons = [...]
function delReason(targetReason){
reasons = reasons.filter((item)=>{
// 把没用匹配的返回,组装成新数组。
return item.id != targetReason.id
})
return reasons
}
删除某个 reason 下指定的 measure。
let reasons = [...]
function delMeasure(targetMeasure){
reasons = reasons.map((item)=>{
item.measures = item.measures.filter((measureItem)=> measureItem.id != targetMeasure.id)
return item
})
return reasons
}
测试数据:
"reasons": [
{
"tenantId": "1268024398774177794",
"createTime": 1642044516649,
"updateTime": 1642161992240,
"updateUser": "1437627170697543683",
"id": "1481468194816217090",
"gapId": "1481468194614890498",
"reasonNo": "BHDB00007",
"reasonContent": "某日某时?#VCM压缩机计划检修,剩余6台压缩机满负荷运行,乙炔通量调整至19000m3/h,单体减产10吨。",
"rate": 99,
"isDel": 0,
"measures": [
{
"tenantId": "1268024398774177794",
"createTime": 1642161992246,
"updateTime": 1642161992246,
"updateUser": "1437627170697543683",
"id": "1481960923185614849",
"gapReasonId": "1481468194816217090",
"measureContent": "计划检修,责任人:XXX;完成时间:X月X日。",
"personId": "1322069368578351150",
"personName": "茹伟",
"joinPerson": "[{\"userId\":\"1322069414707306499\",\"username\":\"王在清(wangzq12)\"}]",
"joinPersonName": "王在清(wangzq12)",
"completeDate": "2022-01-25",
"isTask": "0",
"isDel": 0
},
{
"tenantId": "1268024398774177794",
"createTime": 1642044516688,
"updateTime": 1642157810459,
"updateUser": "1437627170697543683",
"id": "1481468194975600641",
"gapReasonId": "1481468194816217090",
"measureContent": "调整各台转化器配比,及时将失效触媒切出翻到。责任人:王磊;完成时间:X月X日。",
"personId": "1322069368578351150",
"personName": "茹伟",
"joinPerson": "[{\"userId\":\"1339563619613458458\",\"username\":\"贾春伟(jiacw984)\"}]",
"joinPersonName": "贾春伟(jiacw984)",
"completeDate": "2022-01-31",
"isTask": "0",
"isDel": 0
}
],
"targetCode": null
},
{
"tenantId": "1268024398774177794",
"createTime": 1642150622106,
"updateTime": 1642156984212,
"updateUser": "1437627170697543683",
"id": "1481913233361936385",
"gapId": "1481468194614890498",
"reasonNo": "BHDB00021",
"reasonContent": "VCM除汞器乙炔含量超标(当前值?标准值<3%),导致单体耗电石超标。",
"rate": 1,
"isDel": 0,
"measures": [],
"targetCode": null
}
]