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