当前位置: 首页 > 知识库问答 >
问题:

javascript - 前端js 替换问题?

左丘成天
2024-04-10
"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    }]

91c7117d5f72ce895cf92856ffe3780.png

怎么替换成红色的值 对象可能有多个

共有2个答案

裴宜春
2024-04-10

就是写一个 .map() 循环一下咯。

但是不知道你的转换规则是什么,所以只能简单写一个伪代码给你看

const assessingOfficer = [...] // 你的数组数据const newAssessingOfficer = assessingOfficer.map(item => {    return {        ...item,        AssessingStatus: AssessingStatusTransfer(item.AssessingStatus)    }})// 状态转换函数function AssessingStatusTransfer(val){    // 这里是你的转换规则    const newVal = ....    return newVal}
司徒瀚
2024-04-10
const assessingOfficer = [  {    AssessingStatus: 2,  },  {    AssessingStatus: 2,  },  {    AssessingStatus: 1,  },  {    AssessingStatus: 1,  },  {    AssessingStatus: 2,  },  {    AssessingStatus: 2,  },  {    AssessingStatus: 2,  },];var assessingOfficerArr = assessingOfficer.map((el) => el.AssessingStatus);var deepAssessingOfficerArr = JSON.parse(JSON.stringify(assessingOfficerArr));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);

image.png

 类似资料: