Javascript 如何创建多维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7545641/
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
How to create multidimensional array
提问by SpitFire
Can anyone give me a sample/example of JavaScript with a multidimensional array of inputs? Hope you could help because I'm still new to the JavaScript.
谁能给我一个带有多维输入数组的 JavaScript 示例/示例?希望您能提供帮助,因为我还是 JavaScript 的新手。
Like when you input 2 rows and 2 columns the output of it will be 2 rows of input and 2 columns of input.
就像当您输入 2 行 2 列时,它的输出将是 2 行输入和 2 列输入。
Like this:
像这样:
[input][input]
[input][input]
回答by Jared Farrish
var numeric = [
['input1','input2'],
['input3','input4']
];
numeric[0][0] == 'input1';
numeric[0][1] == 'input2';
numeric[1][0] == 'input3';
numeric[1][1] == 'input4';
var obj = {
'row1' : {
'key1' : 'input1',
'key2' : 'input2'
},
'row2' : {
'key3' : 'input3',
'key4' : 'input4'
}
};
obj.row1.key1 == 'input1';
obj.row1.key2 == 'input2';
obj.row2.key1 == 'input3';
obj.row2.key2 == 'input4';
var mixed = {
'row1' : ['input1', 'inpu2'],
'row2' : ['input3', 'input4']
};
mixed.row1[0] == 'input1';
mixed.row1[1] == 'input2';
mixed.row2[0] == 'input3';
mixed.row2[1] == 'input4';
And if you're wanting to store DOM elements:
如果你想存储 DOM 元素:
var inputs = [
[
document.createElement('input'),
document.createElement('input')
],
[
document.createElement('input'),
document.createElement('input')
]
];
inputs[0][0].id = 'input1';
inputs[0][1].id = 'input2';
inputs[1][0].id = 'input3';
inputs[1][1].id = 'input4';
Not real sure how useful the above is until you attach the elements. The below may be more what you're looking for:
在您附加元素之前,不确定上述内容有多大用处。以下可能是您正在寻找的更多内容:
<input text="text" id="input5"/>
<input text="text" id="input6"/>
<input text="text" id="input7"/>
<input text="text" id="input8"/>
var els = [
[
document.getElementById('input5'),
document.getElementById('input6')
],
[
document.getElementById('input7'),
document.getElementById('input8')
]
];
els[0][0].id = 'input5';
els[0][1].id = 'input6';
els[1][0].id = 'input7';
els[1][1].id = 'input8';
Or, maybe this:
或者,也许是这样的:
<input text="text" value="4" id="input5"/>
<input text="text" value="4" id="input6"/>
<br/>
<input text="text" value="2" id="input7"/>
<input text="text" value="4" id="input8"/>
var els = [
[
document.getElementById('input5'),
document.getElementById('input6')
],
[
document.getElementById('input7'),
document.getElementById('input8')
]
];
var result = [];
for (var i = 0; i < els.length; i++) {
result[result.length] = els[0][i].value - els[1][i].value;
}
Which gives:
这使:
[2, 0]
In the console. If you want to output that to text, you can result.join(' ');
, which would give you 2 0
.
在控制台中。如果你想把它输出到文本,你可以result.join(' ');
,这会给你2 0
.
EDIT
编辑
And a working demonstration:
和一个工作演示:
<input text="text" value="4" id="input5"/>
<input text="text" value="4" id="input6"/>
<br/>
<input text="text" value="2" id="input7"/>
<input text="text" value="4" id="input8"/>
<br/>
<input type="button" value="Add" onclick="add()"/>
// This would just go in a script block in the head
function add() {
var els = [
[
document.getElementById('input5'),
document.getElementById('input6')
],
[
document.getElementById('input7'),
document.getElementById('input8')
]
];
var result = [];
for (var i = 0; i < els.length; i++) {
result[result.length] = parseInt(els[0][i].value) - parseInt(els[1][i].value);
}
alert(result.join(' '));
}
回答by Teoman shipahi
Quote taken from Data Structures and Algorithms with JavaScript
引用自Data Structures and Algorithms with JavaScript
The Good Parts (O'Reilly, p. 64). Crockford extends the JavaScript array object with a function that sets the number of rows and columns and sets each value to a value passed to the function. Here is his definition:
好的部分(O'Reilly,第 64 页)。Crockford 使用一个函数扩展了 JavaScript 数组对象,该函数设置行数和列数,并将每个值设置为传递给函数的值。这是他的定义:
Array.matrix = function(numrows, numcols, initial) {
var arr = [];
for (var i = 0; i < numrows; ++i) {
var columns = [];
for (var j = 0; j < numcols; ++j) {
columns[j] = initial;
}
arr[i] = columns;
}
return arr;
}
Here is some code to test the definition:
下面是一些测试定义的代码:
var nums = Array.matrix(5,5,0);
print(nums[1][1]); // displays 0
var names = Array.matrix(3,3,"");
names[1][2] = "Joe";
print(names[1][2]); // display "Joe"
We can also create a two-dimensional array and initialize it to a set of values in one line:
我们还可以创建一个二维数组并将其初始化为一行中的一组值:
var grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];
print(grades[2][2]); // displays 89
回答by ShawnS
Declared without value assignment.
未赋值的声明。
2 dimensions...
2维...
var arrayName = new Array(new Array());
3 dimensions...
3维...
var arrayName = new Array(new Array(new Array()));
回答by Nadav
I know this is ancient but what about...
我知道这是古老的,但那又如何呢……
4x4 example (actually 4x<anything>)
:
4x4 示例(actually 4x<anything>)
:
var matrix = [ [],[],[],[] ]
which can filled by:
可以填写:
for (var i=0; i<4; i++) {
for (var j=0; j<4; j++) {
matrix[i][j] = i*j;
}
}
回答by user4839072
very simple
很简单
var states = [,];
states[0,0] = tName;
states[0,1] = '1';
states[1,0] = tName;
states[2,1] = '1';
. . .
. . .
states[n,0] = tName;
states[n,1] = '1';
回答by Ken Jinks
function Array2D(x, y)
{
var array2D = new Array(x);
for(var i = 0; i < array2D.length; i++)
{
array2D[i] = new Array(y);
}
return array2D;
}
var myNewArray = Array2D(4, 9);
myNewArray[3][5] = "booger";
回答by Nishanth Suresh
Hope the following code suits your requirement
希望以下代码符合您的要求
var row= 20;
var column= 10;
var f = new Array();
for (i=0;i<row;i++) {
f[i]=new Array();
for (j=0;j<column;j++) {
f[i][j]=0;
}
}
回答by Gaurav
var size = 0;
var darray = new Array();
function createTable(){
darray[size] = new Array();
darray[size][0] = $("#chqdate").val();
darray[size][1]= $("#chqNo").val();
darray[size][2] = $("#chqNarration").val() ;
darray[size][3]= $("#chqAmount").val();
darray[size][4]= $("#chqMode").val();
}
increase size var after your function.
在您的功能之后增加大小 var。
回答by BinaryOverdose
So here's my solution.
所以这是我的解决方案。
A simple example for a 3x3 Array. You can keep chaining this to go deeper
一个 3x3 数组的简单示例。你可以继续链接这个以更深入
Array(3).fill().map(a => Array(3))
Or the following function will generate any level deep you like
或者下面的函数会生成你喜欢的任何深度
f = arr => {
let str = 'return ', l = arr.length;
arr.forEach((v, i) => {
str += i < l-1 ? `Array(${v}).fill().map(a => ` : `Array(${v}` + ')'.repeat(l);
});
return Function(str)();
}
f([4,5,6]) // Generates a 4x5x6 Array
http://www.binaryoverdose.com/2017/02/07/Generating-Multidimensional-Arrays-in-JavaScript/
http://www.binaryoverdose.com/2017/02/07/Generating-Multidimensional-Arrays-in-JavaScript/
回答by b1c10
you can create array follow the code below:
您可以按照以下代码创建数组:
var arraymultidimensional = []
arraymultidimensional = [[value1,value2],[value3,value4],[value5,value6]];
Result:
[v1][v2] position 0
[v3][v4] position 1
[v5][v6] position 2
结果:
[v1][v2] 位置 0
[v3][v4] 位置 1
[v5][v6] 位置 2
For add to array dinamically, use the method below:
要动态添加到数组,请使用以下方法:
//vectorvalue format = "[value,value,...]"
function addToArray(vectorvalue){
arraymultidimensional[arraymultidimensional.length] = vectorvalue;
}
Hope this helps. :)
希望这可以帮助。:)