-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicDeepClone.js
More file actions
38 lines (32 loc) · 835 Bytes
/
Copy pathbasicDeepClone.js
File metadata and controls
38 lines (32 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function deepClone(obj) {
if (obj === null || typeof obj !== "object") {
return obj; // primitive or null
}
if (obj instanceof Date) return new Date(obj);
if (obj instanceof RegExp) return new RegExp(obj);
if (Array.isArray(obj)) {
return obj.map((item) => deepClone(item));
}
const result = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = deepClone(obj[key]);
}
}
return result;
}
// Testing
const obj = {
a: "test",
b: { c: 1 },
arr: [1, 2, 3, 4],
arr2: [{ x: "y" }],
d: null,
e: new Date(),
f: /abc/g
};
const obj2 = deepClone(obj);
console.log("obj2", obj2);
console.log("obj === obj2", obj === obj2); // false
console.log("obj.b === obj2.b", obj.b === obj2.b); // false
console.log("obj.arr2 === obj2.arr2", obj.arr2 === obj2.arr2); // false