Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create deep copy in angular 2 [duplicate]

How I can create deep copy in angular 2, I tried to use let newObject = Object.assign({}, myObject) but still myObject reflects all the changes done in newObject.

like image 908
Dheeraj Agrawal Avatar asked Dec 05 '16 04:12

Dheeraj Agrawal


Video Answer


1 Answers

Just use the following function :

/**
 * 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;
}
like image 175
Dev Avatar answered Oct 09 '22 22:10

Dev