深拷贝

会在多维数据中使用,树状数据,不会改变原数组的值
 
// object deep clone
export function deepClone(source) {
if (!source && !typeof source !== 'object') {
throw new Error('error arguments', 'deepClone')
}
const targetObj = source.constructor === Array ? [] : {}
for (const key in source) {
if (source.hasOwnProperty(key)) {
if (source[key] && typeof source[key] === 'object') {
targetObj[key] = source[key].constructor === Array ? [] : {}
targetObj[key] = deepClone(source[key])
} else {
targetObj[key] = source[key]
}
}
}
return targetObj
}
 
原文地址:https://www.cnblogs.com/wszxx/p/9887628.html