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);

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b42a1d-2c61e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b42a1d-2c61e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?