Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回数据的javascript关联数组类型 delta={ “北”:[-1,0,-1,-1,-1,1], “南部”:[1,0,1-1,1,1], “东”:[0,-1,0,-1,0,1], “西”:[0,1,0,1,0,-1] } Tracarray=函数(){ 对于(变量i=0;i_Javascript_Arrays - Fatal编程技术网

返回数据的javascript关联数组类型 delta={ “北”:[-1,0,-1,-1,-1,1], “南部”:[1,0,1-1,1,1], “东”:[0,-1,0,-1,0,1], “西”:[0,1,0,1,0,-1] } Tracarray=函数(){ 对于(变量i=0;i

返回数据的javascript关联数组类型 delta={ “北”:[-1,0,-1,-1,-1,1], “南部”:[1,0,1-1,1,1], “东”:[0,-1,0,-1,0,1], “西”:[0,1,0,1,0,-1] } Tracarray=函数(){ 对于(变量i=0;i,javascript,arrays,Javascript,Arrays,我使用的是一个具有四个字符串键的关联数组,每个关联的数据组件是一个6元素的数字数组。访问时,23个元素返回数值,一个元素始终返回未定义的值。数组定义可能有错误,请指出错误所在。是。这里有一个输入错误,这使得delta.south delta = { "north": [-1, 0, -1, -1, -1, 1], "south": [ 1, 0, 1 -1, 1, 1], "east": [ 0, -1, 0, -1, 0, 1], "wes

我使用的是一个具有四个字符串键的关联数组,每个关联的数据组件是一个6元素的数字数组。访问时,23个元素返回数值,一个元素始终返回未定义的值。数组定义可能有错误,请指出错误所在。

是。这里有一个输入错误,这使得
delta.south

delta = {
    "north": [-1,  0, -1, -1, -1,  1],
    "south": [ 1,  0,  1  -1,  1,  1],
    "east":  [ 0, -1,  0, -1,  0,  1],
    "west":  [ 0,  1,  0,  1,  0, -1]
 }
traceArray = function() {
    for(var i = 0; i < 6; i++) {
        console.log('array() - n,' + i + ' colDelta = ' + typeof this.delta['north'][i]);
        console.log('array() - s,' + i + ' colDelta = ' + typeof this.delta['south'][i]);
        console.log('array() - e,' + i + ' colDelta = ' + typeof this.delta['east'][i]);
        console.log('array() - w,' + i + ' colDelta = ' + typeof this.delta['west'][i]);
    }
}
发生的事情是因为没有
它的计算结果是
1-1=0
,所以
delta.south[2]
等于
0
,所以没有第六个元素是您正在访问的,因此
未定义的

 delta = {
    "north": [-1,  0, -1, -1, -1,  1],
    "south": [ 1,  0,  1,  -1,  1,  1],  // there was a comma missing after 2nd 1
    "east":  [ 0, -1,  0, -1,  0,  1],
    "west":  [ 0,  1,  0,  1,  0, -1]
 }

这里漏了一个逗号,所以这个数组中只有5个元素。这就是为什么在尝试读取第6个元素时会出现
未定义的

这是因为缺少一个逗号:

"south": [ 1,  0,  1  -1,  1,  1],
                    ^
delta={
“北”:[-1,0,-1,-1,-1,1],
“南部”:[1,0,1-1,1,1]//
delta = {
    "north": [-1,  0, -1, -1, -1,  1],
    "south": [ 1,  0,  1  -1,  1,  1], // <- check it here
    "east":  [ 0, -1,  0, -1,  0,  1],
    "west":  [ 0,  1,  0,  1,  0, -1]
 }