The problem of assignment Association of ts Array

recently I was learning ng2, but found a problem with array assignment. I just wanted to assign the value of list1 to list2, but they made an association.

Code:

export class AppComponent {

  public list1 = [];
  public list2 = [];

  constructor() {
    this.list1 = ["1","2"];

    this.list2 = this.list1;

    this.list1.push("3");

    console.log(this.list2);
  }

}

output:

(3)["1", "2", "3"]
Mar.06,2021

this.list2 = this.list1;
this sentence is equivalent to
your house number is "list1", and now there is another house number called "list2", which eventually points to your house. I suggest you make up the knowledge of stack.


simple way:

this.list2.push(...list1);
Menu