I have a little problem with TypeScript. I want to reverse an Array. But before I reverse my Array, I want to write in into a local Storage.
The problem is, that I call the method to store the data before I call the method to reverse my array. But the stored data are already reversed.
Here is an example code and here is the Playground:
class TSRunner {
defaultArray = []
reversedArray = [];
fillArray() {
this.defaultArray = [1, 2, 3, 4, 5];
this.reversedArray = this.defaultArray;
console.log(this.reversedArray);
this.reversedArray.reverse();
}
}
let runner = new TSRunner();
runner.fillArray();
The output is already 5,4,3,2,1. But I want to get the 1,2,3,4,5 and after that, I want to reverse it. Maybe you could explain me this behaviour. I think it has something todo with the call by reference stuff. Thanks before ;)