我尝试创建三维数组并为其赋值,根据答案creating and parsing a 3D array in javascript?。
var myArr = new Array(new Array(new Array()));
myArr[0][0][0] = "1";
myArr[0][0][1] = "2";
myArr[0][1][0] = "2";
myArr[0][1][1] = "2";
myArr[0][2][0] = "3";
myArr[0][2][1] = "4";
但我得到了这个
Uncaught Typeerror:无法设置未定义的“0”属性。
需要一些帮助。
谢谢
答案 0 :(得分:1)
javascript中没有预先启动变量,编译步骤只是提升顶级变量和函数定义,而在ES6中,静态导入和导出(尽管尚未支持)[Patrick Roberts]。另外一个选择是
var myArr = new Array();
myArr[0] = new Array();
myArr[0][0] = new Array();
myArr[0][0][0] = "1";
myArr[0][0][1] = "2";
myArr[0][1] = new Array();
myArr[0][1][0] = "2";
myArr[0][1][1] = "2";
myArr[0][2] = new Array();
myArr[0][2][0] = "3";
myArr[0][2][1] = "4";
答案 1 :(得分:0)
你可以这样做(但是在javascript中创建3d数组是更完整的东西。请考虑改变处理数据的方法):
var myArr = new Array();
myArr[0] = new Array();
myArr[0][0] = new Array();
myArr[0][0][0] = "1";
myArr[0][0][1] = "2";
myArr[0][1] = new Array();
myArr[0][1][0] = "2";
myArr[0][1][1] = "2";
myArr[0][2] = new Array();
myArr[0][2][0] = "3";
myArr[0][2][1] = "4";
console.log(myArr[0][2][0]);