Deep copy an array in Angular 2 + TypeScript

前端 未结 10 1140
温柔的废话
温柔的废话 2020-11-27 04:33

I have an array of objects that is an input. Lets call it content.

When trying to deep copy it, it still has a reference to the previous array.

10条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 05:18

    I found deep copy method in angular devkit, It's very normal, so... maybe you can just implement yourself or use that.

    I prefer to use loadash, there a lot of objects and array operation methods that can use.

    import { deepCopy } from '@angular-devkit/core/src/utils/object';
    
    export class AppComponent {
      source = {
        ....
      }
      constructor() {
         const newObject = deepCopy(this.source);
      }
    }
    
    Package                           Version
    -----------------------------------------------------------
    @angular-devkit/architect         0.1000.8
    @angular-devkit/build-angular     0.1000.8
    @angular-devkit/build-optimizer   0.1000.8
    @angular-devkit/build-webpack     0.1000.8
    @angular-devkit/core              10.0.8
    @angular-devkit/schematics        10.0.8
    @angular/cli                      10.0.8
    @ngtools/webpack                  10.0.8
    @schematics/angular               10.0.8
    @schematics/update                0.1000.8
    rxjs                              6.5.5
    typescript                        3.9.7
    webpack                           4.43.0
    

提交回复
热议问题