To efficiently create deep copies of objects containing nested objects, it is recommended to utilize lodash's cloneDeep method.
When working with Angular, the process can be accomplished in the following manner:
Begin by installing lodash using either yarn add lodash
or npm install lodash
.
Within your component, import the cloneDeep
function and apply it as follows:
import * as cloneDeep from 'lodash/cloneDeep';
...
clonedObject = cloneDeep(originalObject);
Despite adding just 18kb to your project's build, the advantages are certainly worth it.
For further insights on the significance of utilizing lodash's cloneDeep, you can refer to my detailed article here.