1、簡(jiǎn)單實(shí)現(xiàn)
let copy = JSON.parse(JSON.stringify(boj));
2倡怎、高逼格遞歸實(shí)現(xiàn)深拷貝
function copy(obj){
let newObj = obj.constructor === 'Object' ? {} : [];
//判斷是否為對(duì)象類型,不是則返回
if(typeof obj !== 'object'){
return;
}
for(let key in obj){
//判斷子級(jí)是否為對(duì)象贱枣,是則遞歸遍歷
newObj[key] = typeof obj[key] === 'object' ?
copy(obj[key]) : obj[key]
}
return newObject;
}
數(shù)據(jù)過多影響性能监署,有時(shí)候我們僅需對(duì)對(duì)象中某個(gè)值進(jìn)行深拷貝,冯事?焦匈??