Last active
April 14, 2019 00:44
-
-
Save karkranikhil/a44c25d5fab403a66bc00f03bbe342f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function deepCopy(obj){ | |
| const keys = Object.keys(obj) | |
| const newObject={} | |
| for(let i=0;i<keys.length;i++){ | |
| const key = keys[i] | |
| if((!!obj[key]) && (obj[key].constructor === Object)){ | |
| newObject[key] = deepCopy(obj[key]) | |
| } else { | |
| newObject[key] = obj[key] | |
| } | |
| } | |
| return newObject | |
| } | |
| const obj4 ={ | |
| firstName:'nikhil', | |
| lastName:'karkra', | |
| isTeaching: true, | |
| address:{ | |
| state:'victoria', | |
| city:'Melbourne' | |
| } | |
| } | |
| const obj5 = deepCopy(obj4) | |
| obj5.address.city="new city" | |
| console.log(obj4.address.city) // Melbourne | |
| console.log(obj5.address.city) // new city |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment