我在javascript中创建2d数组时遇到问题。这是我的代码:
var arr = new Array([]);
arr[0][0] = "1st data from array";
arr[1][0] = "2nd data from array";
document.write(arr[0][1] + " " + arr[1][0]);
我应该怎么做才能让它发挥作用?
答案 0 :(得分:0)
在设置arr
:
arr[i][x]
中初始化一个项目
var arr = new Array([]);
arr[0] = arr[1] = [];
arr[0][1] = "1st data from array";
arr[1][0] = "2nd data from array";
document.write(arr[0][1] + " " + arr[1][0]);
答案 1 :(得分:0)
它会抛出错误,因为arr [0]和arr [1]尚未设置为数组。尝试:
var arr = [[],[]];