function deepCopy(data) {
if(typeof data !== "object")throw new Error("参数错误");
let res = Array.isArray(data)?[]:{};
for(let key in data){
res[key] = (typeof data[key] === "object") ? deepCopy(data[key]): data[key];
}
return res;
}
let arr = [1,2,3,[4,5,[6,7]]];
let res = deepCopy(arr);
console.log(res,res===arr);
js面试题---数组和对象的深复制?
最新推荐文章于 2024-07-10 14:03:37 发布