Changes to object still reflected after object assign Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Changes to object still reflected after object assign without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I assign oldScore to newScore.

Later on when I update the newScore object, the changes are reflected to oldScore, can someone assist?

The changes made to newScore should not be reflected to oldScore. newScore and oldScore are two distinct and different objects.

words.newScore = Object.assign( {}, words.oldScore );

Thank you

Answer

Object.assign will shallow copy, means toplevel object’s own properties only. May you are trying to update nested object’s values, thus it’s updating old instance too. For that you need to Deep Copy instances.
For more details refer: MDN - Object.assign(): Warning for Deep Clone

We are here to answer your question about Changes to object still reflected after object assign - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji