簡體   English   中英

試圖在函數中創建一個多維數組

[英]Trying to create a multi-dimensional array in a function

我目前正在嘗試將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中的每個元素都是整數。

嗨878 654

嗨878 654

嗨878 654

等等....

這應該會給你你期望的結果:

 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('&emsp;')}<br/>`); }); } exitToTable(a, b, c); 

希望這可以幫助,

 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] + '&emsp;'); } //document.write('</br>'); } console.log(tester); console.log(tester[0][0]); } 

這是如何在JavaScript中創建多維數組的簡單示例

  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); 

我在雇主的幫助下與我一起努力推動我朝着正確方向前進的答案如下:


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>');
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM