utils/deepCopy.js
export default function deepCopy(obj) { // 深拷贝方法
if (typeof obj !== 'object' || obj === null) {
return obj;
}
let copy;
if (obj instanceof Array) {
copy = [];
for (let i = 0; i < obj.length; i++) {
copy[i] = deepCopy(obj[i]);
}
} else {
copy = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
copy[key] = deepCopy(obj[key]);
}
}
}
return copy;
}
引用
import deepCopy from '@/util/deepCopy.js';
const obj = { name: 'Tom', age: 29, hobbies: ['sing', 'jump','rap'] };
const copiedObj = deepCopy(obj);
标签:obj,js,复制,let,key,deepCopy,拷贝,copy
From: https://www.cnblogs.com/axingya/p/17901752.html