0

So I have this unwanted thing that keeps happening when I have arary1 (which contains values that won't change throughout my javascript code) and then an exact copy (upon initialization) of a second array2 that will contain the same variables but this time the numbers will change as the code goes. So if array1 = array2 and array2[x] = 2, then array1[x] also gets the (unwanted) value of 2. To escape this I used for loops but the code gets bigger and bigger. So what I came up with was an HTTP requesting a JSON file with jQuery in this case: $.ajax({ ..type: get....url: www.location.com...datatype: 'json'....success: function {}) and in this function I had array1 and array2 to receive the same data from the JSON file (not necessarily making array1 = array2 ????) so I got really disappointed when I found out that both arrays were being changed at the same time as if array1 = array2. So how can I avoid this? Or what am I doing here wrong? Or is there anything that I should know of?

  • Well `array1 = array2` doesn't make a copy. – Bergi Jul 13 '17 at 00:07
  • Yes, ajax requests are definitely an overkill. JSON serialisation and deserialisation can help to make deep copies, but should not be necessary in general. – Bergi Jul 13 '17 at 00:08

0 Answers0