Wednesday, 20 September 2017

angularjs - Create deep copy in angular 2




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.


Answer




Just use the following function :



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

public deepCopy(oldObj: 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;
}

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...