0

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:

  1. Pushing an empty new variable and setting it's value a line later to the variable that needs to be stored.
  2. Pushing the variable as is into the array.
  3. 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.

CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • 1
    It's not following a "reference variable", it's just that previously you were probably pushing _an object (reference)_ into the array, without creating a copy of the object, so you would have had the same object in both the array and the `dummy1` variable. – CherryDT Feb 07 '23 at 19:38
  • 1
    But nonetheless, the code you showed _does_ create a copy (a new `dummy` object, so now there are two different objects in play) and _does_ behave as you expected, so I don't see the issue. I converted your code to a runnable snippet, so you can click the "Run" button and see for yourself. – CherryDT Feb 07 '23 at 19:39
  • But similar code (only with different names and more properties) doesn't return the desired value. It always changes to the new value of the "reference variable"... – somebody1122 Feb 07 '23 at 19:41
  • Probably one of those "more properties" is itself an object reference. Probably you should create a deep copy before pushing into the array. Note that this is a non-trivial operation which depends on what kind of data and references your objects actually contain. – CherryDT Feb 07 '23 at 19:42
  • I agree with @CherryDT. try doing a deep copy before pushing to the array. or if you dont mind, put a jsfiddle link with actual object which is giving you the hard time and probably people can help better when they can replicate the problem. – sandeep joshi Feb 07 '23 at 19:51
  • Creating a deep copy worked. Thank you! I'm new to Stack Overflow so sorry if this is a silly question, but should I answer my question so people can more easily see the solution? Do you want to do it? – somebody1122 Feb 07 '23 at 19:57
  • I would say go ahead and write a nice answer yourself! glad we could help and Welcome to SO! – sandeep joshi Feb 07 '23 at 20:06

2 Answers2

0

Not sure if this would help, You can make a variable immutable after changing it by using Object.freeze(). Object.freeze() is a method that prevents further changes to an object by making it read-only and non-configurable.

Ashwant Manikoth
  • 355
  • 3
  • 17
0

If your "var" is a object with plain properties, use this for pushing:

data.push({ ...dummy1 })

If not -contains other objects for example- then you need a deep copy as the others already said.