我可以使用一些指导来在javascript中指定动态多维数组。据我所知,javascript本身并不是定义多个dim数组,而是数组的数组,即。
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.
var items = [[1,2],[3,4],[5,6]];或
var array = [[,],[,]]或
var a = [[1,2],[3,4]]我的问题是我不知道实际尺寸,只是定义数组,如上面的第二个例子,仍然不允许数组超越两个记录集。我以为有一个类似于VB的REDIM stmt但是找不到任何东西。
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.
我的另一个问题是当我指定第二个维度时数组,如下例所示,数组在for块之外变得无法访问。
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.
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有105条记录且持平,{12 ,弗兰克,564,......等已经证明了这一点。填充第二条记录后,我收到此错误....
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....
Error: Unable to set value of the property '0': object is null or undefined.显然,我的javascript技能至少可以说有点粗糙。任何示例代码都将不胜感激。
Obviously, my javascript skills are a bit rough to say the least. Any sample code would be greatly appreciated.
谢谢,
推荐答案你不要将数组调暗到实际大小,
You do not Dim the Array to an actual size,
你可以在任何索引上设置一个你想要的变量
You can just set an variable on any index to whatever you want
如果你想要两个Dimensions,你可以定义一个数组
if you want two Dimensions you just could define an array
var arr = []
将元素设置为另一个数组
set an element to another array
arr [0] = []
并在第二维中的元素中放置一个值
and put an value inside an element in the "2nd dimension"
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
喜欢
var arr = [] var arr1 = [1,2,3] arr[0] = arr1 console.log(arr[0][2]) //3如果你有一个数组并想要内部数组中的push元素只是做一个chec k如果元素被定义,例如
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()这对我有用
更多推荐
javascript多维动态数组
发布评论