1

On this simple example (https://onecompiler.com/javascript/3xn7dr7jc) :

var arrayButton = new Array(11);
var arrayAdvancedButtons = new Array(6);

for(let i=0; i < arrayButton.length; i++){
    
    arrayButton[i] = [i, 0];
}


for(let i=0; i < arrayAdvancedButtons.length; i++){
    
    arrayAdvancedButtons[i] = arrayButton;
}

 arrayAdvancedButtons[0][0][1] += 1;


console.log(arrayAdvancedButtons);

With this declaration :

arrayAdvancedButtons[0][0][1] += 1;

All the the array elements in this position [x][0][1] are incremented instead of [0][0][1] element.

What is the reason of this result?

ExecAssa
  • 177
  • 11
  • all of the elements of `arrayAdvancedButtons` are references to the same `arrayButton` array. – pilchard Dec 23 '21 at 12:27
  • 1
    `arrayButton` is one single array which you assign to multiple slots in `arrayAdvancedButtons`. Modifying "one" of the arrays modifies "all" arrays. The quotes are because there is literally just one array you can manipulate from multiple places. – VLAZ Dec 23 '21 at 12:28

0 Answers0