前端 js 替换问题
给定一个 JSON 对象列表:
"assessingOfficer": [ { "AssessingOfficerName": "卡特琳娜", "AssessingStatus": 1, "AssessingOfficerType": 1, "dataTime": "2024-04-09 17:12:04", "remarks": "我们在一起", "UserName": "卡特琳娜" }, { "dataTime": "2024-04-09 17:07:17", "remarks": "222222222222", "UserName": "卡特琳娜", "AssessingOfficerName": "卡特琳娜", "AssessingStatus": 2, "AssessingOfficerType": 2 }, { "dataTime": "2024-04-09 17:07:33", "remarks": "22222222222222", "UserName": "卡特琳娜", "AssessingOfficerName": "卡特琳娜", "AssessingStatus": 2, "AssessingOfficerType": 2 } ]
如何将对象中 AssessingStatus 为 1 的替换成红色值?
可以按照以下步骤进行替换:
// 提取 AssessingStatus 值数组 const assessingOfficerArr = assessingOfficer.map((el) => el.AssessingStatus); // 深拷贝AssessingStatus值数组 const deepAssessingOfficerArr = JSON.parse(JSON.stringify(assessingOfficerArr)); // 替换 AssessingStatus 值 for (let i = 0; i < deepAssessingOfficerArr.length; i++) { if (i === 0) assessingOfficerArr[0] = deepAssessingOfficerArr[0] === 1 ? 3 : deepAssessingOfficerArr[0]; if (i > 0) assessingOfficerArr[i] = deepAssessingOfficerArr[i] === 1 ? 3 : deepAssessingOfficerArr[i - 1] === 1 ? 1 : deepAssessingOfficerArr[i]; } console.log('assessingOfficerArr', assessingOfficerArr);
替换后的结果如下:
assessingOfficerArr [ 3, 3, 1, 3, 3, 3, 3 ]