Deep copy an array in Angular 2 + TypeScript

前端 未结 10 1029
温柔的废话
温柔的废话 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
    
    0 讨论(0)
  • 2020-11-27 05:19

    Alternatively, you can use the GitHub project ts-deepcopy, which is also available on npm, to clone your object, or just include the code snippet below.

    /**
     * Deep copy function for TypeScript.
     * @param T Generic type of target/copied value.
     * @param target Target value to be copied.
     * @see Source project, ts-deepcopy https://github.com/ykdr2017/ts-deepcopy
     * @see Code pen https://codepen.io/erikvullings/pen/ejyBYg
     */
    export const deepCopy = <T>(target: T): T => {
      if (target === null) {
        return target;
      }
      if (target instanceof Date) {
        return new Date(target.getTime()) as any;
      }
      if (target instanceof Array) {
        const cp = [] as any[];
        (target as any[]).forEach((v) => { cp.push(v); });
        return cp.map((n: any) => deepCopy<any>(n)) as any;
      }
      if (typeof target === 'object' && target !== {}) {
        const cp = { ...(target as { [key: string]: any }) } as { [key: string]: any };
        Object.keys(cp).forEach(k => {
          cp[k] = deepCopy<any>(cp[k]);
        });
        return cp as T;
      }
      return target;
    };
    
    0 讨论(0)
  • 2020-11-27 05:20

    This is working for me:

    this.listCopy = Object.assign([], this.list);
    
    0 讨论(0)
  • 2020-11-27 05:23

    A 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.

    0 讨论(0)
提交回复
热议问题