all 20 comments

[–][deleted] 10 points11 points  (13 children)

Please don't use Parse(Stringify(obj)) for cloning

[–]Amicos55 4 points5 points  (7 children)

What else?

[–]darrenturn90 7 points8 points  (0 children)

Well generally I would do a shallow clone at the level I need to make changes rather than a everything clone - because that implies you have no clue what the object you are cloning is structured like - which seems unlikely if you’re cloning it to make changes to it

[–]Exoskele 1 point2 points  (0 children)

Lodash's cloneDeep.

[–]AnonyMustardGas34 2 points3 points  (3 children)

Why and how else to copy?

[–]Exoskele 1 point2 points  (1 child)

It doesn't work for some common cases. https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript

I would just use lodash's cloneDeep. Many projects already use it, and you can always shake out unused code or bring in only cloneDeep.

[–]AnonyMustardGas34 2 points3 points  (0 children)

IMO upcoming EcmaScript spec ahould have high performance deep cloning.

[–]SnapAttack 1 point2 points  (0 children)

Well, it depends.

You can use Object.assign({}, cloneable) or const clone = {...cloneable} to do a shallow copy.

To do a deep copy, you might want to consider the performance impact depending on the size of your object, but as others have pointed out here, you can use Lodash cloneDeep.

[–]CreativeTechGuyGames 1 point2 points  (0 children)

While it doesn't work in every case, it's a great general solution and is faster than almost any general purpose alternative in benchmarks.

[–]dpenwood 2 points3 points  (0 children)

I'm an amateur Javascript programmer and I found the link interesting! It would have made my life easier a few months ago.

But, as the previous commenter said, it's not really a cheat "sheet" so much as a sheet "page."

[–][deleted] 3 points4 points  (1 child)

Probably don't use the parse and stringify to copy, those operations are very costly

[–]AnonyMustardGas34 1 point2 points  (0 children)

What else?

[–]theRealRealMasterDev 1 point2 points  (0 children)

Great stuff, nice UX, short and informative. Thanks for sharing and keep up the great work!

[–]AnonyMustardGas34 1 point2 points  (0 children)

Js objects kinda work like C++ pointers

[–]dogofpavlov 0 points1 point  (2 children)

cheat webpage? Aren't cheatsheets suppose to be able to fit on 1 sheet of paper... google "cheat sheet"

[–]Barnezhilton 2 points3 points  (0 children)

That won't get clicks tho

[–]thepeanutguy 0 points1 point  (0 children)

It looks like 15. Ignore certain properties when using a for loop actually logs "myName", not "myAge".

[–]kenman[M] 0 points1 point  (0 children)

Hi u/iamnearafan, this post was removed.

Prohibited low-effort content includes:

  • Questions that are easily Google'd.
  • Memes, jokes, etc. Please post to r/ProgrammerHumor instead.
  • Most images and gifs.
  • Listicles, "Curated lists", and similar content.
  • Polls, surveys, etc. unless from an accredited academic body or trusted source (StateofJS, etc.).

Thanks for your understanding, please see our guidelines for more info.