87

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.

1

10 Answers 10

193

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))

Sign up to request clarification or add additional context in comments.

4 Comments

this won't work for nested objects. How can we deep copy nested objects?
This would copy an object by reference. What if I do want to copy by reference, so as to avoid any changes in myObject affecting 'copy' object.
@PratapA.K Take a look at loadsh, if you can load another library in your system. It has functions such as _cloneDeep.
'Object' only refers to a type, but is being used as a namespace here.ts(2702)
61

As mentioned above:

let copy = Object.assign({}, myObject);

But this won't work for nested objects. So an alternative would be:

let copy = JSON.parse(JSON.stringify(myObject));

1 Comment

worked for me when nested objects are there, Thank you
14

As suggested before, the clean way of deep copying objects having nested objects inside is by using lodash's cloneDeep method.

For Angular, you can do it like this:

Install lodash with yarn add lodash or npm install lodash.

In your component, import cloneDeep and use it:

import * as cloneDeep from 'lodash/cloneDeep';
...
clonedObject = cloneDeep(originalObject);

It's only 18kb added to your build, well worth for the benefits.

I've also written an article here, if you need more insight on why using lodash's cloneDeep.

1 Comment

That works for me! Thank you :) It seems incredible that high level programming language cannot deal with a "copy" without reference...
11

You can do in this in Angular with ECMAScript6 by using the spread operator:

let copy = {...myObject};

1 Comment

Downvoted because copy will hold a reference to the old object.
5
let course = {
  name: 'Angular',
};

let newCourse= Object.assign({}, course);

newCourse.name= 'React';

console.log(course.name); // writes Angular
console.log(newCourse.name); // writes React

For Nested Object we can use of 3rd party libraries, for deep copying objects. In case of lodash, use _.cloneDeep()

let newCourse= _.cloneDeep(course);

Comments

4

Try this.

Copy an Array :

const myCopiedArray  = Object.assign([], myArray);

Copy an object :

const myCopiedObject = Object.assign({}, myObject);

Comments

2

Loadsh is the universal standard library for coping any object deepcopy. It's a recursive algorithm. It's check everything and does copy for the given object. Writing this kind of algorithm will take longer time. It's better to leverage the same.

Comments

2

Object.assign will only work in single level of object reference.

To do a copy in any depth use as below:

let x = {'a':'a','b':{'c':'c'}};
let y = JSON.parse(JSON.stringify(x));

If want to use any library instead then go with the loadash.js library.

Comments

2

You can now do deep copy without any external library as below:

let originalObj = {'a':'a','b':{'c':'c'}};
let copyObj = structuredClone(originalObj);

Complete Reference MDN

Comments

0

thank you Saif from above. I tried his suggestion and it worked like a charm after I tried for three days to get my code to work.

  this.sElectionFinal.subscribe((election) => {
 const electionCopy = Object.assign({},election)
  this.electionsFinal.push(electionCopy)})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.