Modify the property value of the cloned array object, will the property value of the original array object change accordingly?

I have tried to clone an array using methods such as slice (), concat (), map (), and then use forEach to traverse the array to modify the attribute values of the objects in the array. It is found that the attribute values of the objects in the original array will change accordingly. Is there any way to solve this problem: modify the attribute values of the cloned array objects, and the values of the original array objects can not be changed?

Nov.09,2021

A relatively simple method is JSON.parse (JSON.stringify (arrObj))


The

slice () method is a shallow clone.

how to achieve deep cloning of objects


A solution has been found on the Internet:
/ / method one uses iterative
/ / the idea is to determine whether the object to be cloned is a reference type, if it is a reference type, continue to iterate, and if the item is a basic type, copy it directly.
function deepClone (obj) {

let newObj=Array.isArray(obj)?[]:{}

if(obj&&typeof obj ==="object"){
    for(let key in obj){
        if(obj.hasOwnProperty(key)){
            newObj[key]=(obj && typeof obj[key]==='object')?deepClone(obj[key]):obj[key];
        }
    }
}
return newObj

}

let a = [{br 1}, 2Jing 3jade 4],
b=deepClone (a);
a [0] = {CMV 2};
console.log (AMagol b);

Menu