尝试在函数中创建多维数组

我目前正在尝试采用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] + '&emsp;');

                }

                document.write('</br>');

            }


        }

我希望数组在a ='Hi'b = 878 c = 654时输出如下


但是请注意,“ a”,“ b”和“ c”都是存储自己的数据的数组。a中的每个元素都是一个字符串,b和c中的每个元素都是整数。


嗨878654


嗨878654


嗨878654


等等....


慕虎7371278
浏览 112回答 3
3回答

幕布斯6054654

在雇主的脑力激荡中,我的想法是:我将自己的工作推向正确的方向,如下所示:function generateTable(a, b, c) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.write('<table cellspacing="5" cellpadding="1" border="1"><tr><th>State/Teritory</th><th>population</th><th>Growth Rate</th></tr>')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < a.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.write('<tr><td>' + a[i] + '</td><td>' + b[i] + '</td><td>' + c[i] + '</td></tr>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.write('</table><br><hr>');&nbsp; &nbsp; &nbsp; &nbsp; }

摇曳的蔷薇

这应该给您您期望的结果: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) {&nbsp; let length = a.length;&nbsp; if (length !== b.length || length !== c.length) {&nbsp; &nbsp; console.warn('arrays must be of same length');&nbsp; }&nbsp; [...Array(length)].map((_, index) => {&nbsp; &nbsp; document.write(`${[a[index], b[index], c[index]].join('&emsp;')}<br/>`);&nbsp; });}exitToTable(a, b, c);

慕莱坞森

var arr_a = [],&nbsp;&nbsp; &nbsp; arr_b = [],&nbsp;&nbsp; &nbsp; arr_c = [];for (var i = 0; i <= 7; i++) {&nbsp; arr_a[i] = 'Hi';&nbsp; arr_b[i] = 878;&nbsp; arr_c[i] = 654;&nbsp;}exitToTable(arr_a, arr_b, arr_c);function exitToTable(a, b, c) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let tester = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[0] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[0][0] = a[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[0][1] = b[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[0][2] = c[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[1] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[1][0] = a[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[1][1] = b[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[1][2] = c[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[2] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[2][0] = a[2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[2][1] = b[2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[2][2] = c[2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[3] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[3][0] = a[3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[3][1] = b[3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[3][2] = c[3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[4] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[4][0] = a[4];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[4][1] = b[4];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[4][2] = c[4];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[5] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[5][0] = a[5];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[5][1] = b[5];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[5][2] = c[5];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[6] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[6][0] = a[6];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[6][1] = b[6];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[6][2] = c[6];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[7] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[7][0] = a[7];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[7][1] = b[7];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tester[7][2] = c[7];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < 8; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var j = 0; j < 3; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //document.write(tester[i][j] + '&emsp;');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //document.write('</br>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(tester);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(tester[0][0]);&nbsp; &nbsp; &nbsp; &nbsp; }这是一个简单的示例,说明如何使用JavaScript创建多维数组&nbsp; &nbsp; &nbsp; &nbsp; var array = [];&nbsp; &nbsp; &nbsp; &nbsp; var array2 = [];&nbsp; &nbsp; &nbsp; &nbsp; var array3 = [];&nbsp; &nbsp; &nbsp; &nbsp; array2.push('text-1');&nbsp; &nbsp; &nbsp; &nbsp; array2.push('text-2');&nbsp; &nbsp; &nbsp; &nbsp; array3.push('item-1');&nbsp; &nbsp; &nbsp; &nbsp; array3.push('item-2');&nbsp; &nbsp; &nbsp; &nbsp; array.push(array2);&nbsp; &nbsp; &nbsp; &nbsp; array.push(array3);&nbsp; &nbsp; &nbsp; &nbsp; console.log(array);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; console.log(array[0][1]);找出数组的长度:array.length。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var array = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var array2 = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array2.push('text-1');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array2.push('text-2');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[0] = array2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(array);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript