在角度2中创建深层副本

Dhe*_*wal 8 angularjs angular

如何在角度2中创建深层副本,我尝试使用let newObject = Object.assign({}, myObject)但仍然myObject反映了在newObject中完成的所有更改.

Gui*_*ère 4

只需使用以下函数:

/**
 * Returns a deep copy of the object
 */

public deepCopy(oldObj: any) :any {
    var newObj = oldObj;
    if (oldObj && typeof oldObj === "object") {
        newObj = Object.prototype.toString.call(oldObj) === "[object Array]" ? [] : {};
        for (var i in oldObj)
            newObj[i] = this.deepCopy(oldObj[i]);
    }
    return newObj;
}
Run Code Online (Sandbox Code Playgroud)