作者:Maugham | 来源:互联网 | 2023-10-12 04:31
varoldObj{id:1,name:电脑,brand:[{id:11,name:惠普,price:5600},{id:12,name:苹果,price:9000}],}深层拷贝
var oldObj = {id: 1,name: '电脑',brand: [{id: 11,name: '惠普',price: 5600},{id: 12,name: '苹果',price: 9000}],}// 深层拷贝函数function deepCopy(newObj, oldObj) {for(var k in oldObj) {if(oldObj[k] instanceof Array){// 拷贝数组newObj[k] = []deepCopy(newObj[k], oldObj[k])}else if(oldObj[k] instanceof Object) {// 拷贝对象newObj[k] = {}deepCopy(newObj[k], oldObj[k])}else{// 拷贝简单数据类型newObj[k] = oldObj[k]}}}var newObj = {}deepCopy(newObj, oldObj)