Skip to content

Instantly share code, notes, and snippets.

@OdearOgy
Last active December 3, 2018 08:20
Show Gist options
  • Save OdearOgy/713549146e9a0af5bbf30b6f2ad4a37b to your computer and use it in GitHub Desktop.
Save OdearOgy/713549146e9a0af5bbf30b6f2ad4a37b to your computer and use it in GitHub Desktop.
deep copy function without json.stringify
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