0
myObj : [] = [];
tmpObj: [] = [];

ngOnInit() {

    this.myService.getData().subscribe(
         data => {
          this.myObj = data.values;
          this.tmpObj = Object.assign([], this.myObj);
        }
    )

}

updateData(prop, val) {
    this.myObj[prop] = val;
}

When I call updateData method and update the property of myObj, this change affect the tmpObj. But I don't want the tmObj object to be affected by these changes. What am I supposed to do for this?

midstack
  • 2,105
  • 7
  • 44
  • 73
  • 1
    You need to make a deep copy of the object (array) https://stackoverflow.com/questions/597588/how-do-you-clone-an-array-of-objects-in-javascript – martin Nov 12 '18 at 14:27

1 Answers1

1

if is an array you can do:

  this.myService.getData().subscribe(
         data => {
          this.myObj = data.values;
          this.tmpObj = data.values.slice();
        }
    )

or you can do:

this.myService.getData().subscribe(
         data => {
          this.myObj = data.values;
          this.tmpObj = data.values.map(x => Object.assign({}, x))
        }
    )
lesiano
  • 288
  • 3
  • 9