let obj={
a:1,
b:{
b1:1,
b2:2
},
c:[1,2,3]
}
function deepClone(obj){
let result=Array.isArray(obj)?[]:{}
for(let key in obj){
if(obj.hasOwnProperty(key)){
if(obj[key]&&typeof obj[key]=='object'){
result[key]=deepClone(obj[key])
}else{
result[key]=obj[key]
}
}
}
return result
}
let objClone=deepClone(obj)
objClone.b.b1='b1'
objClone.c=[4,5,6]
console.log("obj:",obj);
console.log("objClone:",objClone);
标签:obj,递归,objClone,js,let,result,key,拷贝,b1
From: https://www.cnblogs.com/772330747wh/p/17491236.html