Last active
August 12, 2020 11:46
-
-
Save rodrigograca31/0e94078b80b016d0db37d1195a03755c to your computer and use it in GitHub Desktop.
Deep copy objects in Javascript
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
/** | |
* Makes a copy of values not references and goes down the attributes | |
*/ | |
deepCopy(o) { | |
var copy = o, | |
k; | |
if (o && typeof o === "object") { | |
copy = | |
Object.prototype.toString.call(o) === "[object Array]" | |
? [] | |
: {}; | |
for (k in o) { | |
copy[k] = this.deepCopy(o[k]); | |
} | |
} | |
return copy; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment