Open ankeji opened 4 years ago
const deepClone = (data) => {
var obj;
if (isArray(data)) {
obj = [];
} else if (isObject(data)) {
obj = {};
} else {
//不再具有下一层次
return data;
}
if (isArray(data)) {
for (var i = 0, len = data.length; i < len; i++) {
obj.push(deepClone(data[i]));
}
} else if (isObject(data)) {
for (var key in data) {
obj[key] = deepClone(data[key]);
}
}
return obj;
};
>
function demo(obj) { if (typeof obj !== 'object') return obj; let result = obj instanceof Array ? [] : {}; for (const key in obj) { result[key] = typeof obj[key] === 'object' ? demo(obj[key]) : obj[key]; } return result; }
function deepClone(obj){ var rest=JSON.parse(JSON.sringify(obj)); return rest; }
只能一级拷贝
发自我的iPhone
在 2021年3月22日,11:36,soliderMan @.***> 写道:
function deepClone(obj){ var rest=JSON.parse(JSON.sringify(obj)); return rest; }
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.
function deepClone(obj) { if (typeof obj !== 'object' || obj == null) { return obj } var res if (Array.isArray(obj)) { res = [] } else { res = {} } for (const key in obj) { if (obj.hasOwnProperty(key)) { res[key] = deepClone(obj[key]) } } return res }
function deepClone(obj = {}) {
if (typeof obj !== "object" || obj == null) return obj;
let result = obj instanceof Array ? [] : ({});
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = deepClone(obj[key]);
}
}
return result;
}
function deepClone(obj, hash = new WeakMap()) {
if (obj === null) {
return obj
}
if (obj instanceof Date) {
return new Date(obj)
}
if (obj instanceof RegExp) {
return new RegExp(obj)
}
if (obj instanceof Error) {
return new Error(obj.message)
}
if (typeof obj !== 'object') {
return obj
}
// 防止循环引用
if (hash.has(obj)) {
return hash.get(obj)
}
hash.set(obj, obj)
let newObj = Object.create(null)
const keys = Reflect.ownKeys(obj)
for (const key of keys) {
const val = obj[key]
if (typeof val === 'object') {
newObj[key] = deepClone(val, hash)
} else {
newObj[key] = val
}
}
return newObj
}
function deepClone(obj, hash = new WeakMap()) { if (obj === null) { return obj } if (obj instanceof Date) { return new Date(obj) } if (obj instanceof RegExp) { return new RegExp(obj) } if (obj instanceof Error) { return new Error(obj.message) } if (typeof obj !== 'object') { return obj } // 防止循环引用 if (hash.has(obj)) { return hash.get(obj) } hash.set(obj, obj) let newObj = Object.create(null) const keys = Reflect.ownKeys(obj) for (const key of keys) { const val = obj[key] if (typeof val === 'object') { newObj[key] = deepClone(val, hash) } else { newObj[key] = val } } return newObj }
Array
const deepCopy = (originObj, property) => { const map = new WeakMap(); function dp(obj) { const result = Array.isArray(obj) ? [] : {}; const existObj = map.get(obj); if (existObj) { return existObj; } map.set(obj, result); for (let key of Reflect.ownKeys(obj)) { // 只需要加一个检测,看看key是不是我们需要的属性就行 if (obj.hasOwnProperty(key) && key === property) { if (obj[key] && typeof obj[key] === 'object') { result[key] = dp(obj[key]) } else { result[key] = obj[key]; } } } return result; } return dp(originObj); }