Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2: how to copy object into another object

Please help me in order to copy the object into another object using angular 2?

In angular, I used angular.copy() to copy objects to the loose reference of the old objects. But, when I used the same in angular 2 getting below error:

Error: angular is not defined.

like image 811
Swetha Avatar asked Sep 15 '16 08:09

Swetha


People also ask

How do I copy an object to another object in TypeScript?

Use the Object. assign() Method to Clone an Object in TypeScript. The Object. assign() works similarly as in the case of the spread operator and can be used to clone simple objects, but it fails in the case of nested objects.

How do I assign one object to another in TypeScript?

The Object. assign() function can be used to copy all enumerable own properties from one or more source objects to a target object. This function returns the target object to the newObject variable. Here's an example of copying by assigning in TypeScript.

How do you copy an object in Javascript?

var clone = Object. assign({}, obj); The Object. assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object.


1 Answers

Solution

Angular2 developed on the ground of modern technologies like TypeScript and ES6.

So you can just do let copy = Object.assign({}, myObject).

Object assign - nice examples.

For nested objects : let copy = JSON.parse(JSON.stringify(myObject))

like image 104
Mikki Kobvel Avatar answered Sep 23 '22 18:09

Mikki Kobvel