Hi Tod Gentille, thanks so much for stopping by with the useful example! This is a great little bit of TypeScript code you shared:

Here would be a similar implementation in JavaScript for interested readers:

If I’m understanding this correctly, this will only deep copy the first layer, but it does not recurse to any more-deeply nested objects or array.

I’m sure it would not take much more code to use .map() and .slice() recursively to deep copy an array, though.

I love the clarity of the example, combining .map() and .slice() like that.

Well done, and thanks for sharing!

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store