AngularJS必须angular.copy()深度复制对象和数组。
Angular是否也有类似的东西?
AngularJS必须angular.copy()深度复制对象和数组。
Angular是否也有类似的东西?
Duplicate也许,但是我想要一个non-polyfill解决方案。就像angular.copy()
                angular.copy()。我应该删除问题吗?
                Answers:
您还可以使用:
JSON.parse(JSON.stringify(Object)) 
如果在您的范围内,则在每个Angular组件,指令等中,并且在每个节点环境中。
除非您有循环引用,否则它应该起作用并将有效地将变量引用与原始对象分离。
Object.assing({}, oldObject, JSON.parse(JSON.stringify(oldObject))),这将从对象文字中重新填充函数属性,并且使用JSON将创建深层副本而无需与原著有任何关系。
                    这个问题不是我如何在angular 2中使用angular.copy的重复,因为OP正在询问深复制对象。链接的答案推荐不进行深层复制的Object.assign()。
其实用Angular2不使用其他库,例如限制你的jQuery深拷贝与他们的对象$ .clone()函数或lodash与_.cloneDeep() 。
最常见的库可通过键入CLI工具提供其键入,因此即使从TypeScript进行编译,您也可以无缝使用所需的任何内容。
另一种选择是实现自己的功能:
/**
 * Returns a deep copy of the object
 */
public static deepCopy(oldObj: any) {
    var newObj = oldObj;
    if (oldObj && typeof oldObj === "object") {
        if (oldObj instanceof Date) {
           return new Date(oldObj.getTime());
        }
        newObj = Object.prototype.toString.call(oldObj) === "[object Array]" ? [] : {};
        for (var i in oldObj) {
            newObj[i] = this.deepCopy(oldObj[i]);
        }
    }
    return newObj;
}
Create helper class with name deepCopy.ts
/*
* DeepCopy class helps to copy an Original Array or an Object without impacting on original data
*/
export class DeepCopy {
  static copy(data: any) {
    let node;
    if (Array.isArray(data)) {
      node = data.length > 0 ? data.slice(0) : [];
      node.forEach((e, i) => {
        if (
          (typeof e === 'object' && e !== {}) ||
          (Array.isArray(e) && e.length > 0)
        ) {
          node[i] = DeepCopy.copy(e);
        }
      });
    } else if (data && typeof data === 'object') {
      node = data instanceof Date ? data : Object.assign({}, data);
      Object.keys(node).forEach((key) => {
        if (
          (typeof node[key] === 'object' && node[key] !== {}) ||
          (Array.isArray(node[key]) && node[key].length > 0)
        ) {
          node[key] = DeepCopy.copy(node[key]);
        }
      });
    } else {
      node = data;
    }
    return node;
  }
}
根据需要导入deepCopy文件,并使用以下代码 DeepCopy.copy(arg); ,这里arg将是您想要的对象或数组
如果源是对象数组,请使用map:
let cloned = source.map(x => Object.assign({}, x));
要么
let cloned = source.map((x) => {
                return { ...x };
             });
对KrishnamrajuK答案的一些修改
export class DeepCopy {
  static copy(data: any, objMap?: WeakMap<any, any>) {
    if (!objMap) {
      // Map for handle recursive objects
      objMap = new WeakMap();
    }
    // recursion wrapper
    const deeper = value => {
      if (value && typeof value === 'object') {
        return DeepCopy.copy(value, objMap);
      }
      return value;
    };
    // Array value
    if (Array.isArray(data)) return data.map(deeper);
    // Object value
    if (data && typeof data === 'object') {
      // Same object seen earlier
      if (objMap.has(data)) return objMap.get(data);
      // Date object
      if (data instanceof Date) {
        const result = new Date(data.valueOf());
        objMap.set(data, result);
        return result;
      }
      // Use original prototype
      const node = Object.create(Object.getPrototypeOf(data));
      // Save object to map before recursion
      objMap.set(data, node);
      for (const [key, value] of Object.entries(data)) {
        node[key] = deeper(value);
      }
      return node;
    }
    // Scalar value
    return data;
  }
}
我面临着深复制的问题。angular.copy({},factory)和angular.extend({},factory)对于数组对象或哈希对象很有帮助,但是在复制对象时,有时会存在连接依赖项问题。我解决了这个问题,所以:
 copyFactory = (() ->
    resource = ->
      resource.__super__.constructor.apply this, arguments
      return
    this.extendTo resource
    resource
  ).call(factory)
我在Typescript中创建了一个非常简单的函数,该函数接受所有可能的输入并提供该对象的深层克隆副本。
希望它能帮助到别人。
  public deepCopy(obj) {
    var clonedObject: any;
    if (obj instanceof Array) {
        var itemArray = Object.assign([], obj);
        clonedObject = itemArray;
        for (var j = 0; j < clonedObject.length; j++) {
            clonedObject[j] = this.deepCopy(clonedObject[j]);
        }
        return clonedObject;
    }
    else if (typeof obj === 'number' || typeof obj == 'string') {
        return obj
    }
    else {
        var item = Object.assign({}, obj);
        clonedObject = item;
        let allKeys = Object.keys(clonedObject);
        for (var i = 0; i < allKeys.length; i++) {
            if (clonedObject[allKeys[i]] instanceof Array) {
                //If the calue is Array
                clonedObject[allKeys[i]] = this.deepCopy(clonedObject[allKeys[i]]);
            }
            else if (clonedObject[allKeys[i]] instanceof Date) {
                clonedObject[allKeys[i]] = new Date(clonedObject[allKeys[i]].valueOf());
            }
            else if (clonedObject[allKeys[i]] instanceof Object){
                //if the value is JOBJECT.
                clonedObject[allKeys[i]] = this.deepCopy(clonedObject[allKeys[i]]);
            }
        }
        return clonedObject;
    }
}