Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Farzad YZ
Farzad YZ

Posted on • Originally published atfarzadyz.com

     

3 Ways to clone objects in Javascript

Cloning an object is somewhat a task that is almost always used in any project from a simple one to the most complicated ones. As it may seem simple for not seasoned Javascript developers, it actually has some pitfalls that would hurt you in the bones if you didn’t know the proper way to do it.

Talk is cheap, show me the code

The first way that could cross a developer’s mind is to deeply iterate through the source object’s properties and copy them one by one on the target object. As it may seem good at the beginning, it is not a performance friendly solution and a potential bottleneck when it comes to large and deep objects.

1: Shallow copy using iteration

functioniterationCopy(src){lettarget={};for(letpropinsrc){if(src.hasOwnProperty(prop)){target[prop]=src[prop];}}returntarget;}constsource={a:1,b:2,c:3};consttarget=iterationCopy(source);console.log(target);// {a:1, b:2, c:3}// Check if clones it and not changing itsource.a="a";console.log(source.a);// 'a'console.log(target.a);// 1
Enter fullscreen modeExit fullscreen mode

So as you see, it’s working!

Now let’s cut the chase to the second solution which is indeed more elegant but, more limited to use.

2: Converting to JSON and back

functionjsonCopy(src){returnJSON.parse(JSON.stringify(src));}constsource={a:1,b:2,c:3};consttarget=jsonCopy(source);console.log(target);// {a:1, b:2, c:3}// Check if clones it and not changing itsource.a="a";console.log(source.a);// 'a'console.log(target.a);// 1
Enter fullscreen modeExit fullscreen mode

Note: Be careful about using this method as your source object MUST be JSON safe. So it may need some sort of exception handling to keep it safe in cases in which the source object is not convertible to JSON.

3: UsingObject.assign

Update: This method has a flaw that it only does a shallow copy. It means that nested properties are still going to be copied by reference. Be careful about it.

This way is the best and the safest way I personally consume in my projects. It’s leveraging a built-in static method on the Object object and is handled and provided by the language. So use this one!

functionbestCopyEver(src){returnObject.assign({},src);}constsource={a:1,b:2,c:3};consttarget=bestCopyEver(source);console.log(target);// {a:1, b:2, c:3}// Check if clones it and not changing itsource.a="a";console.log(source.a);// 'a'console.log(target.a);// 1
Enter fullscreen modeExit fullscreen mode

Conclusion

You must know that every framework and including libraries such as Lodash and Underscore has a way to support cloning objects. Almost all of them used a more complex version of iterationCopy before ES6 was introduced. On ES6+ environments, there is language support forObject.assign, so try to use the most out of it. As a rule of thumb, always try to use the 3rd solution and avoid the JSON solution.

Keep calm and clone objects without any hesitation 😊

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Software Engineer @epicgames. formerly @futurice.Javascript, Typescript, React(native), International speaker. UI engineering, Statecharts, Reactivity.
  • Location
    Helsinki
  • Joined

More fromFarzad YZ

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp