其他分享
首页 > 其他分享> > 深度拷贝对象与数组

深度拷贝对象与数组

作者:互联网

function deepCopy(target) {
let result;
if (Object.prototype.toString.call(target) === "[object Array]") {
result = [];
target.forEach((element) => {
result.push(deepCopy(element));
});
} else if (Object.prototype.toString.call(target) === "[object Object]") {
result = {};
Object.keys(target).forEach((key) => {
result[key] = deepCopy(target[key]);
});
} else {
result = target;
}
return result;
}

标签:key,target,Object,toString,result,数组,深度,deepCopy,拷贝
来源: https://www.cnblogs.com/huayang1995/p/16177565.html