I need to be able to record a previously existing variable in an array, and it should remain unchanged after the fact. The current problem is that whenever I set the variable to the value of another variable, it "follows" the value of the "reference variable". How can I prevent this from happening?
I have tried:
- Pushing an empty new variable and setting it's value a line later to the variable that needs to be stored.
- Pushing the variable as is into the array.
- Using constants. This just froze the array and made me unable to record anything on it...
Sample code:
class dummy {
constructor (val1, val2) {
this.val1 = val1
this.val2 = val2
}
}
let data = []
let dummy1 = new dummy(1, 2)
data.push(new dummy(dummy1.val1, dummy1.val2)) // Keep in my that I have tried other ways of doing this.
dummy1.val1 = 3
dummy1.val2 = 4
console.log(dummy1)
console.log(data[0])
Desired output:
3, 4
1, 2
Current output:
3, 4
3, 4
Please, no alternatives to using an array.