// 深度拷贝Array
var deepCopyArray = function(sourceArray){
let target = [];
for (let i = 0; i<sourceArray.length; i++) {
if (judgeisArrayOrObject(sourceArray[i]) == 'array') {
target.push(deepCopyArray(sourceArray[i]))
} else if (judgeisArrayOrObject(sourceArray[i]) == 'object') {
target.push(deepCopyObject(sourceArray[i]));
} else {
target.push(sourceArray[i]);
}
}
return target;
}
// 深度拷贝Object
var deepCopyObject = function(sourceObj){
let obj = {};
for (let key in sourceObj) {
if (judgeisArrayOrObject(sourceObj[key]) == 'object') {
obj[key] = sourceObj[key] ? deepCopyObject(sourceObj[key]) : null;
} else if (judgeisArrayOrObject(sourceObj[key]) == 'array') {
obj[key] = deepCopyArray(sourceObj[key]);
} else {
obj[key] = sourceObj[key];
}
}
return obj
}
// 判断是对象类型
var judgeisArrayOrObject = function(source) {
if (typeof source == 'object' && Array.isArray(source)) {
return 'array'
}
return typeof(source)
}
转载于:https://my.oschina.net/u/1430310/blog/1477190