Created
August 29, 2017 12:46
-
-
Save sawantakash321/4d3dcb2e0c11da2b22372397aaa568c9 to your computer and use it in GitHub Desktop.
A function called deepClone which takes an object and creates a copy of it.
This file contains 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
var personalInfo = { | |
name: 'paddy', | |
address: { | |
town: 'Lerum', | |
country: 'Sweden', | |
} | |
} | |
//function to deep[nested] clone a object | |
function deepClone(object) { | |
var tempObject = {}; | |
for (var key in object) { | |
if (typeof object[key] === 'object') { | |
tempObject[key] = deepClone(object[key]); | |
} else { | |
tempObject[key] = object[key]; | |
} | |
} | |
return tempObject; | |
} | |
var clonedObject = deepClone(obj); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment