I'm doing some operation on a variable modifying its data but I'd like to save it in another variable to be able to restore it if needed. I tried to declare my two variables in my class as below
user: User;
savedUser: User;
and to assign them in the constructor
this.user = data.user;
this.savedUser = data.user;
I have a function in the OnInit block that does the user operation
ngOnInit(): void {
this.user = this.formatUser(this.user);
}
formatUser(user:User): User{
user.name = 'Peter';
user.age = 18;
return user;
}
resetUser(): void {
this.user = this.savedUser;
}
The reset function is not working as it seems the "savedUser" variable is also updated when the line this.user = this.formatUser(this.user); runs
What am I doing wrong?
Thanks