0

I've written that code on VSCODE, IONIC compiler V6.0.0, Angular V13.2.2:

export class OrderDetailNewPage implements OnInit {

  public id:number;
  tischContent = [
    {
        "anzahl": 0.5,
        "groesse": 0.5,
        "name": "test5",
        "preis": 5.5,
        "$token": "-N2mToYDj-i8ToErmaUj"
    }
];
  newTischContent  =[];
  neuerBezahlvorgang:[] = [];
  constructor() { }

  ngOnInit() {
  }


  pushBestellung(itemkey, indexOfElement){
    console.log(this.neuerBezahlvorgang);
    console.log(this.tischContent[0].anzahl);
    this.neuerBezahlvorgang.push(this.tischContent[0]);
    this.neuerBezahlvorgang[0].anzahl = Math.random();
    console.log(this.tischContent[0].anzahl);

  }
}

Here is the log - pushBestellung is triggered when every time you click on page

I want to push in the pushBestellung() function to push the content into the neuerBezahlvorgang[] array. but it changes the tischContent[] array too. WHY? Please Help.

jps
  • 20,041
  • 15
  • 75
  • 79
  • 1
    `this.neuerBezahlvorgang[0]` contains a reference to `this.tischContent[0]`. Both point to the same object in memory. – jabaa Jun 20 '22 at 08:00
  • @jabaa is right, so you just need to recreate object by using `Object.assign()` method which copies all `enumerable own properties` for example `this.neuerBezahlvorgang.push(Object.assign({}, this.tischContent[0]));` – Mohit Sharma Jun 20 '22 at 08:10

0 Answers0