var arr = [1,2,3]; arr[1][1] = 2; alert(a[1][1]); 

Why undefined and how to add an element to a multidimensional array?

  • @websav, 1. You specify a one-dimensional array, and then you try to work with it as with multidimensional. If we transform, then humanly: arr [1] = []; arr [1] [1] = 2; 2. You specify arr , and you alert a . - etki

2 answers 2

 var arr = [1,2,3]; //Или так: arr[1] = []; arr[1].push(2);//arr[1][0] = 2; //Или так: arr[1] = {1:2};//arr[1][1] = 2; 
  • The second option is wonderful, thank you. - websav
  • Fine. Sorry: arr[1] = []; missed in the first version. - knes
 var arr = [1,2,3]; alert(arr[1]); // 2