# javascript - new - 多维数组

## 我如何在JavaScript中创建二维数组? (20)

1. 我如何在JavaScript中声明2维数组？ （假设它是可能的）

2. 我将如何访问其成员？ （ `myArray[0][1]``myArray[0,1]` ？）

# 如何创建一个空的二维数组（单行）

``````Array.from(Array(2), () => new Array(4))
``````

2和4分别是第一和第二维度。

Array.from（arrayLike [，mapFn [，thisArg]]）

``````var arr = Array.from(Array(2), () => new Array(4));
arr[0][0] = 'foo';
console.info(arr);``````

## 或者（但`n = 10,000`效率更低，为 12％）

``````Array(2).fill(null).map(() => Array(4))
``````

``````var arr = Array(2).fill(null).map(() => Array(4));
arr[0][0] = 'foo';
console.info(arr);``````

## 跟进

`````` Array(2).fill(Array(4));
``````

``````var arr = Array(2).fill(Array(4));
arr[0][0] = 'foo';
console.info(arr);
console.info(arr[0][0], arr[1][0]);``````

## 使用数组理解

``````var rows = [1, 2, 3];
var cols = ["a", "b", "c", "d"];

var grid = [ for (r of rows) [ for (c of cols) r+c ] ];

/*
grid = [
["1a","1b","1c","1d"],
["2a","2b","2c","2d"],
["3a","3b","3c","3d"]
]
*/
``````

``````var default = 0;  // your 2d array will be filled with this value
var n_dim = 2;
var m_dim = 7;

var arr = [ for (n of Array(n_dim)) [ for (m of Array(m_dim) default ]]
/*
arr = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
]
*/
``````

`var md = []; for(var i=0; i<5; i++) { md.push(new Array(5).fill(null)); }`

``````var blocks = [];

blocks[0] = [];

blocks[0][0] = 7;
``````

``````function createArray(length) {
var arr = new Array(length || 0),
i = length;

if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[length-1 - i] = createArray.apply(this, args);
}

return arr;
}

createArray();     // [] or new Array()

createArray(2);    // new Array(2)

createArray(3, 2); // [new Array(2),
//  new Array(2),
//  new Array(2)]
``````

``````var arr = [1, 2, [3, 4], 5];

``````

``````var a = [];
while(a.push([]) < 10);
``````

``````// creates 8x8 array filed with "0"
const arr2d = Array.from({ length: 8 }, () => Array.from({ length: 8 }, () => "0"));
``````

``````// creates 8x8 array filed with "0"
const arr2d = Array(8).fill(0).map(() => Array(8).fill("0"));
``````

``````var x = new Array(10);
for (var i = 0; i < 10; i++) {
x[i] = new Array(20);
}
x[5][12] = 3.0;
``````

``````var array = [[,],[,]]
``````

``````var a = [[1,2],[3,4]]
``````

``````var array1 = [[]];
array1[0][100] = 5;

``````

``````/*
*   Function to create an n-dimensional array
*
*   @param array dimensions
*   @param any type value
*
*   @return array array
*/
function createArray(dimensions, value) {
// Create new array
var array = new Array(dimensions[0] || 0);
var i = dimensions[0];

// If dimensions array's length is bigger than 1
// we start creating arrays in the array elements with recursions
// to achieve multidimensional array
if (dimensions.length > 1) {
// Remove the first value from the array
var args = Array.prototype.slice.call(dimensions, 1);
// For each index in the created array create a new array with recursion
while(i--) {
array[dimensions[0]-1 - i] = createArray(args, value);
}
// If there is only one element left in the dimensions array
// assign value to each of the new array's elements if value is set as param
} else {
if (typeof value !== 'undefined') {
while(i--) {
array[dimensions[0]-1 - i] = value;
}
}
}

return array;
}

createArray([]);              // [] or new Array()

createArray([2], 'empty');    // ['empty', 'empty']

createArray([3, 2], 0);       // [[0, 0],
//  [0, 0],
//  [0, 0]]
``````

``````var myDoubleArray = [[]];
``````

``````var testArray1 = [9,8]
var testArray2 = [3,5,7,9,10]
var testArray3 = {"test":123}
var index = 0;

myDoubleArray[index++] = testArray1;
myDoubleArray[index++] = testArray2;
myDoubleArray[index++] = testArray3;

console.log(myDoubleArray[0],myDoubleArray[1][3], myDoubleArray[2]['test'],)
``````

``````[ 9, 8 ] 9 123
``````

``````var arr  = [];

var arr1 = ['00','01'];
var arr2 = ['10','11'];
var arr3 = ['20','21'];

arr.push(arr1);
arr.push(arr2);
arr.push(arr3);

``````

``````var myArray = [[]];
``````

``````function Create2DArray(rows) {
var arr = [];

for (var i=0;i<rows;i++) {
arr[i] = [];
}

return arr;
}
``````

``````var arr = Create2DArray(100);

arr[50][2] = 5;
arr[70][5] = 7454;
// ...
``````

``````var makeArray = function (dims, arr) {
if (dims[1] === undefined) {
return new Array(dims[0]);
}

arr = new Array(dims[0]);

for (var i=0; i<dims[0]; i++) {
arr[i] = new Array(dims[1]);
arr[i] = makeArray(dims.slice(1), arr[i]);
}

return arr;
}
``````

``````var array = makeArray([2, 3, 4, 2]);
``````

``````var appVar = [[]];
appVar[0][4] = "bineesh";
appVar[0][5] = "kumar";
console.log(appVar[0][4] + appVar[0][5]);
console.log(appVar);``````

``````function createArray(x, y) {
return Array.apply(null, Array(x)).map(e => Array(y));
}
``````

``````function createArray(x, y) {
return Array.apply(null, Array(x)).map(function(e) {
return Array(y);
});
}
``````