Javascript two dimensional arrays [duplicate]

一世执手 提交于 2019-12-31 02:52:04

问题


I have declared a two-dimensional array, like so:

a = [[]]

However, when I try to give a second dimension value using a first dimension index other than 0, it doesn't work:

a[1][0] = "foo" //returns error

Is there a better way around this than manually defining every index you need as an array, i.e.:

a[1] = [];
a[2] = [];
a[3] = [];
//et cetera

回答1:


N-Dimensional arrays do not exist in javascript - you have to just make arrays containing arrays as elements.

You're getting an error because a = [[]]; declares an array with one element, which happens to also be an array. Therefore a[0] is the internal array, but a[1] does not exist because you never declared it. The easiest way to properly declare a "two dimensional array" would be to use a loop:

var outerArray = [];
var numInternalArrays = 5;
for (var i = 0; i < numInternalArrays; i++) {
    outerArray[i] = [];
}



回答2:


If you know how many elements the root array should have you could do something like this:

var arr = 
    (Math.pow(2,10)-1).toString(2)  // Binary string of 1s. Its length being 10
    .split('')                      // Create an array from this string
    .map(function(){return [];});   // Map a new empty array to each index

console.log(arr);                   // [[],[],[],[],[],[],[],[],[],[]]

This accomplishes the same thing:

for(var arr = [], i=10; i--; arr[i]=[]);

No need to declare arr outside of the for-loop since javascript doesn't have block scope, it will be added to the scope in which it is executed.




回答3:


a = [[]]

This is an Array, with the first item being an array. Which is why indexing into the first item still works (a[0][0]).

If you want to access the second item as an array, you need to create your array as

a = [[],[]]

See this question for examples of How can I create a two dimensional array in JavaScript?




回答4:


If I understand correctly, use a loop:

for (var i = y; i--; a[i] = []);



回答5:


There are no multidimensional arrays in javascript.
What you are doing is an array of arrays, but the outermost array has only one element (i.e. element 0) whose value is another array. So a[1] (or more generally a[1][x]) is invalid since the outermost array has only one element.
So you can do a[0][x] = "foo" but not the other way around.

So you can either initialize the array with a for loop or do something like var a =[[][][][][]];




回答6:


You can have the array of arrays start as in:

var a = []; // start with the column array

Then when you want to put something in location [i][j] we can call 'i' the row-index and 'j' the column-index.

if (!a[i]) {   // check for row existing
    a[i] = []; // .. and create it if not 
}
a[i][j] = 'foo'; // put something in the array cell

Note that this only works because we are always putting something in the new row array right after we create it. It might not work if you put 0 or "" in there instead of 'foo'.

There are a lot of things in javascript that are 'false' including 'null' and 'undefined' and '0' and I just don't know if an empty array or an array with one element that is an empty string are considered false. So you would have to do some experimenting with how, exactly to detect a missing row array so you can add it in.



来源:https://stackoverflow.com/questions/14510225/javascript-two-dimensional-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!