js 深拷贝
let obj = {
name: '张三',
user: {
name: 'hdcms',
},
data: [],
};
function copy(object) {
let obj = object instanceof Array ? [] : {};
for (const [k, v] of Object.entries(object)) {
obj[k] = typeof v == 'object' ? copy(v) : v;
}
return obj;
}
let ls = copy(obj);
ls.data.push('李四');
console.log(JSON.stringify(ls, null, 2));
console.log(JSON.stringify(obj, null, 2));