Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function deepCopy(obj, hash = new WeakMap()) {
- if (obj === null) return null;
- if (typeof obj !== 'object') return obj;
- if (obj instanceof Date) return new Date(obj);
- if (obj instanceof RegExp) return new RegExp(obj.source, obj.flags);
- if (hash.has(obj)) return hash.get(obj);
- const result = Array.isArray(obj) ? [] : {};
- hash.set(obj, result);
- Object.keys(obj).forEach(key => {
- if (typeof obj[key] === 'object' && obj[key] !== null) {
- result[key] = deepCopy(obj[key], hash);
- } else {
- result[key] = obj[key];
- }
- });
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement