javascript - Why am I getting an error with this 2d array? -


var arr = [[],[]]; var si = 5; var c  = 0; if (arr[si][c] == null) {      arr[si][c] = {            code : "test",      }; } alert(arr[si][c].code); 

hello, trying run sample code getting error, saying attribute "0" of undefined can not called.

the awkward thing if use numeric values instead of variables "si" , "c" index, error doesn't show up!

is possible in js can not use variables index? think work non 2 dimensional array.

thank , best regards

javascript doesn't have concept of 2 dimensional arrays. arrays contain other arrays.

arr[si][c] arr[5][0].

arr array 2 members (0 , 1), each of array.

when access arr[5] undefined because have gone beyond end.

undefined[0] error.

the awkward thing if use numeric values instead of variables "si" , "c" index, error doesn't show up!

you same error if use literals instead of variables. presumably working test involved different numbers.

var arr = [[],[]];  if (arr[5][0] == null)  {       arr[5][0] = {             code : "test",       };  }  alert(arr[5][0].code);


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -