angular2: how to copy object into another object

后端 未结 8 695
失恋的感觉
失恋的感觉 2021-01-30 10:05

Please help me in order to copy the object into another object using angular 2?

In angular, I used angular.copy() to copy objects to the loose reference of the old object

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 10:33

    As suggested before, the clean way of deep copying objects having nested objects inside is by using lodash's cloneDeep method.

    For Angular, you can do it like this:

    Install lodash with yarn add lodash or npm install lodash.

    In your component, import cloneDeep and use it:

    import * as cloneDeep from 'lodash/cloneDeep';
    ...
    clonedObject = cloneDeep(originalObject);
    

    It's only 18kb added to your build, well worth for the benefits.

    I've also written an article here, if you need more insight on why using lodash's cloneDeep.

提交回复
热议问题