I am using Angular 9 and am trying to add multiple objects in an array. But I see that on adding the new object the existing objects also gets changed. I suspect that is because the array has a reference to the object.
How do I change that.
Code:
userModel: UserModel = new UserModel()
userArray: any = new Array()
.....codes..constructors....oninits etc...
submit(){
this.userModel.name = "Hello"
this.userModel.age = "25"
this.userModel.area = "IN"
this.userArray.push(this.userModel)
this.userModel.name = "World"
this.userModel.age = "26"
this.userModel.area = "WB"
this.userArray.push(this.userModel)
}
The Output is
[{'name':'World, 'age':'26', 'area':'WB'},{'name':'World, 'age':'26', 'area':'WB'}]
What I want is
[{'name':'Hello, 'age':'25', 'area':'IN'},{'name':'World, 'age':'26', 'area':'WB'}]