贴代码,为什么我本人写的这个递归最初扭转原数据的值,被拷贝的数据也会扭转(没变成深拷贝)
const deepCopy = (instance) => { const result = Array.isArray(instance) ? [] : instance instanceof Object ? {} : null if (result === null) { throw new Error(`须要传入Array或Object类型的参数,但当初是${typeof instance}类型的参数`) } if (Array.isArray(instance)) { for (let i = 0; i < instance.length; i++) { const inst = instance[i] result[i] = inst if (inst instanceof Object) { deepCopy(inst) } } } else if (instance instanceof Object) { for (const key in instance) { result[key] = instance[key] if (Array.isArray(instance[key]) || instance[key] instanceof Object) { deepCopy(instance[key]) } } } return result } const o = JSON.parse('{"hnzhghActivity":{"activityType":"4","activityTitle":"测试","startTime":"2022-09-01 00:00:00","endTime":"2022-10-25 00:00:00","imgId":"282203357600632832","imgPath":"https://le/2022/09/02/bae50aff-28a3-487a-b99c-1fec2c7ad761.png","createUser":"258054318004981760","unionId":"248601951852867584","unionName":"河南省总工会","createUserName":"lvyuhui"},"answerRule":"阿瓦达瓦办法","answerNum":5,"answerTime":30,"answerFrequency":1,"correctNumber":1,"hnzhghActivityAnswerAwardList":[{"awardType":"2","awardName":"谢谢参加","awardImg":"htile/picture/else.png"},{"awardType":"1","awardName":"23","awardTotal":"23","awardImg":"https://ghile/2022/09/02/1af9e8ae-447e-4ddf-a3b9-6b2839c3209f.png","awardImgId":"282203439435698176","awardProbability":"23","deliveryType":"1"},{"awardType":"1","awardName":"23","awardTotal":"232","awardImg":"https://gile/2022/09/02/49be25a5-e2a1-4a74-a3c8-497f9d327a30.png","awardImgId":"282203467839524864","awardProbability":"3","deliveryType":"2","address":"发回给你跟儿童边呢人"}],"hnzhghActivityAnswerTopicList":[{"topicId":"275063568107655168","topicName":"《工会法》比赛题库0813"},{"topicId":"273491974549753856","topicName":"809题库测试845"},{"topicId":"271339739891978240","topicName":"测试专用题库"}],"topicName":"《工会法》比赛题库0813,809题库测试845,测试专用题库"}') console.log(o); console.log(deepCopy(o));
能够尝试扭转递归后返回的数据,能扭转原数据