cloneDeep
Create a deep copy of an object or array
Usage
Deeply clone the given object or array. The only nested objects that get cloned by default are: plain objects, arrays, Map
instances, and Set
instances.
The default behavior aims to support the most popular use cases. See “Customized cloning” below if you need more control.
By default, non-enumerable properties and computed properties are copied lossless-ly. Note that you can opt out of this behavior if you need better performance (see “Faster cloning” below).
Faster cloning
You can pass the FastCloningStrategy
for better performance, but bear in mind the following tradeoff.
All plain objects and class instances are cloned with {...obj}
. This means that the original prototype, computed properties, and non-enumerable properties are not preserved.
Also note that built-in, complex objects like RegExp
and Date
are still not cloned with this cloning strategy. You can override the cloneOther
function if you need to clone these object types.
Customized cloning
“Cloning strategies” control how certain object types are handled by cloneDeep
. You can pass in a custom strategy, which may even be a partial strategy. Any undefined methods in your strategy will inherit the default logic. Your custom methods can return null
to use the default logic, or they can return the received object to skip cloning.
If you clone the object in your custom method, make sure to pass the clone into the track
function before cloning the nested objects. Here’s an example with cloneOther
that handles a custom class instance.