let arr = ["张三", "李四", "王五",[1,2,3,4,5]]; function deepCopy(obj1) { let obj2 = Array.isArray(obj1) ? [] : {}; for (let key in obj1) {
// hasOwnProperty : 自身属性 会返还 true ,否则会返还false if(obj1.hasOwnProperty(key)){ if(typeof obj1[key] === 'object'){ obj2[key] = deepCopy(obj1[key]); }else{ obj2[key] = obj1[key]; } } } return obj2; } let arr2 = deepCopy(arr); console.log(arr,arr2);
标签:obj1,arr,obj2,let,key,deepCopy,手写,拷贝 From: https://www.cnblogs.com/wh024/p/17426380.html