【问题标题】:Trying to create a multi-dimensional array in a function试图在函数中创建多维数组
【发布时间】:2019-09-09 08:13:05
【问题描述】:

我目前正在尝试获取 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

等....

【问题讨论】:

  • 你好。为函数exitToTable(a, b, c) 添加数据示例。
  • 感谢您的建议。现在完成了。
  • 看,我更新了答案。

标签: javascript arrays function multidimensional-array dynamic


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在我的雇主和我一起头脑风暴的帮助下,我得到的答案是:

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

    【讨论】:

      【解决方案3】:

      这应该会给你预期的结果:

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

      希望这会有所帮助,

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-07
        • 2019-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多