File tree 1 file changed +10
-11
lines changed
1 file changed +10
-11
lines changed Original file line number Diff line number Diff line change @@ -79,34 +79,33 @@ Object.setPrototypeOf(newObj, obj);
79
79
- Deep copy is done by copying all the properties of the object to another object
80
80
81
81
``` js
82
- const x = Object .assign ({}, obj);
82
+ Object .assign ({}, obj);
83
83
```
84
84
85
85
``` js
86
- const x = { ... obj};
86
+ { ... obj};
87
87
```
88
88
89
89
``` js
90
- const x = JSON .parse (JSON .stringify (obj));
90
+ JSON .parse (JSON .stringify (obj));
91
91
```
92
92
93
93
``` js
94
94
function deepCopy (obj ){
95
- if (! obj) return obj;
95
+ if (! obj)
96
+ return obj;
97
+
96
98
const copyObj = {};
97
99
for (const key in obj){
98
- let val;
99
- if (typeof obj[key] !== ' object' )
100
- val = obj[key];
100
+ if (typeof obj[key] !== ' object' || Array .isArray (obj[key]))
101
+ copyObj[key] = obj[key];
101
102
else
102
- val = deepCopy (obj[key]);
103
-
104
- copyObj[key] = val;
103
+ copyObj[key] = deepCopy (obj[key]);
105
104
}
106
105
return copyObj;
107
106
}
108
107
109
- const x = deepCopy (obj);
108
+ deepCopy (obj);
110
109
```
111
110
112
111
###### Notes
You can’t perform that action at this time.
0 commit comments