Last active
December 3, 2018 08:20
-
-
Save OdearOgy/713549146e9a0af5bbf30b6f2ad4a37b to your computer and use it in GitHub Desktop.
deep copy function without json.stringify
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
function iterationCopy(src) { | |
let target = {}; | |
for (let prop in src) { | |
if (src.hasOwnProperty(prop)) { | |
target[prop] = src[prop]; | |
} | |
} | |
return target; | |
} | |
const source = {a:1, b:2, c:3}; | |
const target = iterationCopy(source); | |
console.log(target); // {a:1, b:2, c:3} | |
// Check if clones it and not changing it | |
source.a = 'a'; | |
console.log(source.a); | |
console.log(target.a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment