0

I'm subscribing to an API call using the following code:

this.af.database.list("URL")
    .subscribe((lineList) => {
      this.lineList = lineList;
      this.fullLineList = lineList;
});

Whenever I access this.lineList in my component, this.fullLineList changes too.

How I can store a variable which stores lineList and is note affected by this.lineList changes?

TheUnreal
  • 21,504
  • 41
  • 145
  • 251

1 Answers1

0

With slice() you can create a copy of the list:

this.af.database.list("URL")
.subscribe((lineList) => {
      this.lineList = lineList;
      this.fullLineList = lineList.slice();
});

See also https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

Günter Zöchbauer
  • 558,509
  • 191
  • 1,911
  • 1,506