Deep copy an array in Angular 2 + TypeScript

前端 未结 10 1138
温柔的废话
温柔的废话 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:14

    Here is my own. Doesn't work for complex cases, but for a simple array of Objects, it's good enough.

      deepClone(oldArray: Object[]) {
        let newArray: any = [];
        oldArray.forEach((item) => {
          newArray.push(Object.assign({}, item));
        });
        return newArray;
      }
    

提交回复
热议问题