我目前正在尝试采用3个单独的输入,并将它们转换为多维数组。
我知道用户最多可以输入8个输入,但是我当前的代码会打印出空数组。我正在寻找一种动态创建我编写的代码以避免此问题的方法。下面是我制作的函数的副本。
a = 'Hi'
b = 878
c = 654
function exitToTable(a, b, c) {
for (var i = 0; i < 8; i++) {
tester[i] = new Array(8);
}
tester[0][0] = a[0];
tester[0][1] = b[0];
tester[0][2] = c[0];
tester[1][0] = a[1];
tester[1][1] = b[1];
tester[1][2] = c[1];
tester[2][0] = a[2];
tester[2][1] = b[2];
tester[2][2] = c[2];
tester[3][0] = a[3];
tester[3][1] = b[3];
tester[3][2] = c[3];
tester[4][0] = a[4];
tester[4][1] = b[4];
tester[4][2] = c[4];
tester[5][0] = a[5];
tester[5][1] = b[5];
tester[5][2] = c[5];
tester[6][0] = a[6];
tester[6][1] = b[6];
tester[6][2] = c[6];
tester[7][0] = a[7];
tester[7][1] = b[7];
tester[7][2] = c[7];
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 3; j++) {
document.write(tester[i][j] + ' ');
}
document.write('</br>');
}
}
如果a ='Hi'b = 878 c = 654,我希望数组打印如下:
但是请注意,“ a”,“ b”和“ c”都是存储自己数据的数组。 a中的每个元素都是字符串,b和c中的每个元素都是整数。
嗨878654
嗨878654
嗨878654
ETC ....
答案 0 :(得分:0)
这应该给您您期望的结果:
const a = ['Hi', 'Hi', 'g', 'j', 'm', 'p', 's', 'v'];
const b = ['900', '900', 'h', 'k', 'n', 'q', 't', 'w'];
const c = ['654', '654', 'i', 'l', 'o', 'r', 'u', 'x'];
function exitToTable(a, b, c) {
let length = a.length;
if (length !== b.length || length !== c.length) {
console.warn('arrays must be of same length');
}
[...Array(length)].map((_, index) => {
document.write(`${[a[index], b[index], c[index]].join(' ')}<br/>`);
});
}
exitToTable(a, b, c);
希望这会有所帮助,
答案 1 :(得分:0)
var arr_a = [],
arr_b = [],
arr_c = [];
for (var i = 0; i <= 7; i++) {
arr_a[i] = 'Hi';
arr_b[i] = 878;
arr_c[i] = 654;
}
exitToTable(arr_a, arr_b, arr_c);
function exitToTable(a, b, c) {
let tester = [];
tester[0] = [];
tester[0][0] = a[0];
tester[0][1] = b[0];
tester[0][2] = c[0];
tester[1] = [];
tester[1][0] = a[1];
tester[1][1] = b[1];
tester[1][2] = c[1];
tester[2] = [];
tester[2][0] = a[2];
tester[2][1] = b[2];
tester[2][2] = c[2];
tester[3] = [];
tester[3][0] = a[3];
tester[3][1] = b[3];
tester[3][2] = c[3];
tester[4] = [];
tester[4][0] = a[4];
tester[4][1] = b[4];
tester[4][2] = c[4];
tester[5] = [];
tester[5][0] = a[5];
tester[5][1] = b[5];
tester[5][2] = c[5];
tester[6] = [];
tester[6][0] = a[6];
tester[6][1] = b[6];
tester[6][2] = c[6];
tester[7] = [];
tester[7][0] = a[7];
tester[7][1] = b[7];
tester[7][2] = c[7];
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 3; j++) {
//document.write(tester[i][j] + ' ');
}
//document.write('</br>');
}
console.log(tester);
console.log(tester[0][0]);
}
var array = [];
var array2 = [];
var array3 = [];
array2.push('text-1');
array2.push('text-2');
array3.push('item-1');
array3.push('item-2');
array.push(array2);
array.push(array3);
console.log(array);
console.log(array[0][1]);
找出数组的长度:array.length
。
var array = [];
var array2 = [];
array2.push('text-1');
array2.push('text-2');
array[0] = array2;
console.log(array);
答案 2 :(得分:0)
在雇主的头脑风暴下,我向正确的方向推动我的答案如下:
function generateTable(a, b, c) {
document.write('<table cellspacing="5" cellpadding="1" border="1"><tr><th>State/Teritory</th><th>population</th><th>Growth Rate</th></tr>')
for (var i = 0; i < a.length; i++) {
document.write('<tr><td>' + a[i] + '</td><td>' + b[i] + '</td><td>' + c[i] + '</td></tr>');
}
document.write('</table><br><hr>');
}