Can someone be my saviour and help me with this simple code,that just seem doesn't work as intended

Evening guys,i making a a generator for assigning matches for each team,long story short,this was supposed to be the final part of completing it,i made some lines of code,i spend more than 4 hours,andi just can't figured out why the life of me the result of this,i narrow down the issue and is the code i'm having problem with:

◆Script:$gameVariables.setValue(14, [1, 2, 3]);
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(61, $gameVariables.value(14));
◆Comment:stop
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(62, $gameVariables.value(14));

so i made an array in variables 14,as you can see,before the comment "stop",array 14 will be assign to array 61 which is (1,3,2),when i execute the code before the stop comment,there's no issue,
but execute as a whole,then that's the problem,the 2nd one is true,variable 14 return (1,2,3) and yes just like the code intended,but,when opening console log(not sure what it's named) by pressing F9,both variable 61and 62 are (1,2,3) ,any last variable will replace all the previous assign variable,let add another one on above

◆Script:$gameVariables.setValue(14, [1, 2, 3]);
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(61, $gameVariables.value(14));
◆Comment:stop
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(62, $gameVariables.value(14));
◆Comment:stop
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(63, $gameVariables.value(14));

now i'm assign into 3 variables,which is 61,62,63

at variable 61,the value of variable 14 is (1,3,2) = variable 61 is now (1,3,2)
at variable 62,the value of variable 14 is (1,2,3) = problem is both variable 61 and 62 is (1,2,3) despite i did not assign it to variable 62
at variable 63,the value of variable 14 is (1,3,2) = now all 3 variable ,variable 61,variable 62, and variable 63 has same value (1,3,2) and not just variable 63

what am i doing it wrong,if this is indeed a bug,then i'm really am screwed,this is the last step for me to finish creating league table for sports,if anyone have better code or find flaws on the code above,that would really really helps me a lot.I will reply as soon as i can get since i might be at work.

I've moved this thread to Learning Javascript. Thank you.

It's these lines causing the confusion:
Code:
$gameVariables.setValue(61, $gameVariables.value(14));
$gameVariables.setValue(62, $gameVariables.value(14));
JS uses pointers for assignment of non-primitive types. This means that if you have, say, a variable A and an array B and you try to set one equal to another:
Code:
A = B;
This will result in A and B "pointing" to the same object. Any changes to one will result in the other being changed, because technically they are both looking at the same object. 👋

To get around this with arrays, use the Array.slice method:
Code:
A = B.slice();
This basically makes a new array with the same element values as B, then assigns A to that new array. Since the objects are now distinct, you can change one without affecting the other. 😂
Code:
$gameVariables.setValue(61, $gameVariables.value(14).slice());

Be warned that slice is only a "shallow" copy, i.e. it doesn't make a copy of any elements of the array that are objects themselves. Not an issue in this case, since your array is purely numbers, but I thought I'd mention it. 🙏
It's these lines causing the confusion:
Code:
$gameVariables.setValue(61, $gameVariables.value(14));
$gameVariables.setValue(62, $gameVariables.value(14));
JS uses pointers for assignment of non-primitive types. This means that if you have, say, a variable A and an array B and you try to set one equal to another:
Code:
A = B;
This will result in A and B "pointing" to the same object. Any changes to one will result in the other being changed, because technically they are both looking at the same object. 👋

To get around this with arrays, use the Array.slice method:
Code:
A = B.slice();
This basically makes a new array with the same element values as B, then assigns A to that new array. Since the objects are now distinct, you can change one without affecting the other. 😂
Code:
$gameVariables.setValue(61, $gameVariables.value(14).slice());

Be warned that slice is only a "shallow" copy, i.e. it doesn't make a copy of any elements of the array that are objects themselves. Not an issue in this case, since your array is purely numbers, but I thought I'd mention it. 🙏
Thank you so much!

Original thread: https://forums.rpgmakerweb.com/threads/can-someone-be-my-saviour-and-help-me-with-this-simple-code-that-just-seem-doesnt-work-as-intended.107675/