简体   繁体   English

使用javascript创建多维数组

[英]create multidimensional array by using javascript

This is a very strange to me actually. 其实这对我来说很奇怪。 I can't figure out how create a multidimensional array dynamically. 我不知道如何动态创建多维数组。 I want to create a three-level drop-down list, so I think I need a three dimensional array to store the values. 我想创建一个三级下拉列表,所以我想我需要一个三维数组来存储值。 How to create three dimensional array? 如何创建三维数组?

<script language=javascript>

var aCourse=new Array();
aCourse[0]=new Array();
aCourse[1]=new Array();
aCourse[2]=new Array();
aCourse[3]=new Array();

aCourse[0][0]="Select...";
aCourse[1][0]="Select...";
aCourse[1][1]="T Square";
aCourse[1][2]="V Square";
aCourse[1][3]="S Square";
aCourse[1][4]="B Square";
aCourse[1][5]="G Square";
aCourse[2][0]="Select...";
aCourse[2][1]="N Square";
aCourse[2][2]="W Square";
aCourse[3][0]="Select…";
aCourse[3][1]="J Square";
aCourse[3][2]="M Square";

function ChangeCourse()
{
var i,
iCategoryIndex;
iCategoryIndex=document.frm.optCategory.selectedIndex;
iCourseCount=0;
while (aCourse[iCategoryIndex][iCourseCount]!=null)
iCourseCount++;
document.frm.optCourse.length=iCourseCount;
for (i=0;i<=iCourseCount-1;i++)
document.frm.optCourse[i]=new Option(aCourse[iCategoryIndex][i]);
document.frm.optCourse.focus();
}

</script>
<body ONfocus=ChangeCourse()>
<h3>Choose category…</h3>
<form name="frm">
<p>Category:
<select name="optCategory" size="1" onChange=ChangeCourse()>
<option>Select…</option>
<option>Soccer</option>
<option>Cricket</option>
<option>Rugby</option>
</select>
</p>
<p>Fields
<select name="optCourse" size="1">
<option>Select…</option>
</select>
</p>
</form>

If your only problem is creating the three-dimensional array, this should work: 如果您唯一的问题是创建三维数组,则此方法应该起作用:

var aCourse = [];

for(var i = 0; i < dimensionOneSize; i++) {
  aCourse[i] = [];
  for(var k = 0; k < dimensionTwoSize; k++)
    aCourse[i][k] = [];
}

It is good to note that JavaScript supports the short-hand definition of an array with square brackets [] , so you can use those to make the dynamic definition cleaner. 值得一提的是,JavaScript支持带有方括号[]的数组的简短定义,因此您可以使用它们来简化动态定义。

Possibly Shorter 可能更短

Also, I'm not sure if this works very well, but it might: 另外,我不确定这是否效果很好,但是可能:

var aCourse = [[[]]];

So far, my testing has not proven a way to use this, but it does validate as a proper script. 到目前为止,我的测试尚未证明使用此功能的方法,但是它确实可以作为适当的脚本进行验证。

First, it's always good to initialize arrays using square brackets, [] : 首先,使用方括号[]初始化数组总是好事:

var aCourse = [];

And to simulate multidimensional arrays in JS, which is essentially an indeterminate number of arrays inside of a single array, do this: 要模拟JS中的多维数组,而JS多维数组本质上是单个数组中不确定数量的数组,请执行以下操作:

aCourse[0] = [];

Or you could create the arrays inside of the square brackets when creating the array: 或者,您可以在创建数组时在方括号内创建数组:

var aCourse = [
    [], [], []
];

This is equivalent to: 这等效于:

aCourse[0] = [];
aCourse[1] = [];
aCourse[2] = [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM