Javascript javascript多维动态数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13725138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript multi dimensional dynamic array
提问by htm11h
I could use some guidance on specifying a dynamic multi dimensional array in javascript. I understand that javascript does not natively define multi dim arrays, but rather an array of an array, ie.
我可以使用一些关于在 javascript 中指定动态多维数组的指导。我知道 javascript 本身并没有定义多维数组,而是一个数组的数组,即。
var items = [[1,2],[3,4],[5,6]];
or
或者
var array = [[,],[,]]
or
或者
var a = [[1,2],[3,4]]
My issue is that I do not know the actual dimensions, and simply defining the array, as in the second example above, still does not allow for the array to go beyond two record sets. I thought there was a REDIM stmt similar to VB but have not been able to locate anything.
我的问题是我不知道实际的维度,并且简单地定义数组,如上面的第二个示例,仍然不允许数组超出两个记录集。我以为有一个类似于 VB 的 REDIM stmt,但找不到任何东西。
One additional part of my problem is that when I specify the second dimension of the array, as in the example below, the array becomes unreachable outside of the for block.
我的问题的另一个部分是,当我指定数组的第二维时,如下例所示,数组在 for 块之外变得无法访问。
var Exforsys=new Array(4)
for (i=0; i <4; i++) {
Exforsys[i]=new Array(4)
}
I am trying to retrieve data from my specific array like this...
我正在尝试像这样从我的特定数组中检索数据...
function newTest() {
var myArray = [[],[]];
// myArray[] = new Array(14);
var recCount = coordsAry.length / 15;
var n =0;
var i = 0;
var s = 0;
for (i = 0; i < recCount; i++) {
for (s = 0; s < 15; s++) {
// myArray[i] = new Array(14);
myArray[i][s] = coordsAry[n];
n++;
// alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
// var u = 4;
s = 0;
alert(myArray[0][3]);
alert(myArray[0][4]);
alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
coordsAry has 105 records and is flat, {"12","Frank","564", ... etc} this has been proven. After the 2nd record is populated I get this error....
coordsAry 有 105 条记录并且是平坦的,{"12","Frank","564", ... etc} 这已被证明。填充第二条记录后,我收到此错误....
Error: Unable to set value of the property '0': object is null or undefined.
Obviously, my javascript skills are a bit rough to say the least. Any sample code would be greatly appreciated.
显然,至少可以说,我的 javascript 技能有点粗糙。任何示例代码将不胜感激。
Thanks,
谢谢,
回答by Moritz Roessler
You do not Dim the Array to an actual size,
您没有将数组变暗到实际大小,
You can just set an variable on any index to whatever you want
您可以将任何索引上的变量设置为您想要的任何内容
if you want two Dimensions you just could define an array
如果你想要二维,你可以定义一个数组
var arr = []
var arr = []
set an element to another array
将一个元素设置为另一个数组
arr[0] = []
arr[0] = []
and put an value inside an element in the "2nd dimension"
并在“第二维”的元素内放置一个值
arr[0][10] = 10
arr[0][10] = 10
You only have the elements in the "first dimension" to be an array
你只有“第一维”中的元素是一个数组
You could also do things like placing arrays into arrays
您还可以执行诸如将数组放入数组之类的操作
like
喜欢
var arr = []
var arr1 = [1,2,3]
arr[0] = arr1
console.log(arr[0][2]) //3
And if you have an array and want to push elements in an inner array just do a check if the element is defined, like
如果您有一个数组并想将元素推送到内部数组中,只需检查该元素是否已定义,例如
var arr = []
for ( var i = 0; i < 5 ; i++) {
if(!arr[i])
arr[i] = []
for ( var j = 0; j < 3 ; j++)
arr[i][j] = i + " - " + j // Or like this,
//arr[i].push(i + " " + j) //
}
console.log( arr[2][3]) // 2 - 3
Given the Code you posted
鉴于您发布的代码
function newTest() {
var myArray = [[],[]];
// myArray[] = new Array(14);
var recCount = 15
var n =0;
var i = 0;
var s = 0;
for (i = 0; i < recCount; i++) {
if(!myArray[i])
myArray[i] = []
for (s = 0; s < 15; s++) {
console.log(i)
// myArray[i] = new Array(14);
myArray[i][s] = s;
n++;
// alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
// var u = 4;
s = 0;
alert(myArray[0][3]);
alert(myArray[0][4]);
alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
}
newTest()
This works for me
这对我有用
?
?
回答by Wutz
All arrays are dynamic in javascript. You can do things like
JavaScript 中的所有数组都是动态的。你可以做这样的事情
var list = [];
list[15] = 15;
And it will simply expand the array to have a length of 16, with the values for the first 15 indexes being undefined (~ null). Thus, your 3 examples of multi-dimensional arrays should all work.
它将简单地将数组扩展为长度为 16,前 15 个索引的值未定义(~ null)。因此,您的 3 个多维数组示例都应该有效。
Edit:Just saw that you included your code. You are not initializing the arrays of your first dimension. Your loop should be
编辑:刚刚看到您包含了您的代码。您没有初始化第一维的数组。你的循环应该是
for (i = 0; i < recCount; i++) {
//If the outer array does not have an inner array at this position already, create one
if (!myArray[i])
myArray[i] = []; //!!!
for (s = 0; s < 15; s++) {
// myArray[i] = new Array(14);
myArray[i][s] = coordsAry[n]; //myArray[i] returned null/undefined, when i got bigger then the declared array.
n++;
// alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
// var u = 4;
s = 0;
alert(myArray[0][3]);
alert(myArray[0][4]);
alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
回答by Michoel
To add an element to an array you can use push(val) (to add the back of the array) or unshift(val) to add to the front of the array. So:
要将元素添加到数组,您可以使用 push(val)(添加数组的后面)或 unshift(val) 来添加到数组的前面。所以:
var Exforsys=[];
for (i=0; i <4; i++) {
Exforsys.push([]);
}

