我可以在javascript中拆分预切片的字符串吗?

时间:2015-11-15 12:16:21

标签: javascript arrays split slice

您好我有一项技术测试,要求我将180个字符分成6组,然后将它们分成6个表。

由于我几天前来到这里的帮助,我已经将角色拆分了,但是我意识到我需要再次拆分新形成的阵列;一旦创建了表,这6个组就会将自己登录到6个表中第一个的顶行。

我需要将这些组成对记录到9列x 3行表中,我似乎无法让它们工作。这是我的代码:

 <script>

 var ticketString ="011722475204365360702637497481233455758302154058881928446789061241507324334876840738576186051132437816395663800818206590104559628214294664710935667287132130687703253151692742547985".match(/.{1,2}/g);

 function loadTickets () {

 var ticket = [];

 for (var i = 0; i < ticketString.length / 15; i++) {

     ticket[i] = ticketString.slice(i * 15, (i + 1) * 15).sort();

     var table = document.createElement("TABLE")

     table.border='1'

     table.setAttribute("id","ticketTable" + i)

     var tableBody = document.createElement("TBODY")

          tableBody.setAttribute("id","ticketBody")

          table.appendChild(tableBody); 

                 for (b=0;b<=2;b++) {

                 var row = document.createElement("TR")

                 row.setAttribute("id", "ticketRow" + b)

                 tableBody.appendChild(row)

                     for (c=0; c<=8;c++) {

                     var cell = document.createElement("TD")

                     cell.setAttribute("id","ticket-number" + c)

                     cell.bgColor = 'red';

                     row.appendChild(cell)

                     document.body.appendChild(table)

                      document.getElementById('ticket-number' + i).innerHTML = ticket[i];


     }

 }

 }

 console.log(tableBody);

 console.log(row);

 console.log(cell);

 };

 </script>

1 个答案:

答案 0 :(得分:1)

我不确定你是如何确定空盒子的,但这是6张表,每行有3行和5个数字,就像你的照片一样。基本上它执行以下操作:

  1. 将字符串拆分为18个数组,每个数组包含5个数字。
  2. 创建在#1中创建的上述3个数组的多维数组数组。
  3. 根据确定数字是否属于该索引的算法检查每个位置。
  4. 为每个外部数组创建一个表,为该表中的每个内部数组创建一行,并为该表中每行内的每个数字创建一列。
  5. 在视觉上你可以把它想象成堆叠多维数组。

     var ticketString = "011722475204365360702637497481233455758302154058881928446789061241507324334876840738576186051132437816395663800818206590104559628214294664710935667287132130687703253151692742547985".match(/.{1,2}/g);
    
     function loadTickets() {
         var ticketTables = [];
         var ticketGroups = [];
         var numberOfNumbers = 5; // per row
         var inc = 0;
         for (var i = 0; i < ticketString.length / numberOfNumbers; i++) {
             var start = i * numberOfNumbers;
             var end = start + numberOfNumbers;
             ticketGroups[i] = ticketString.slice(start, end);
    
             ticketTables[inc] = ticketTables[inc] || [];
             ticketTables[inc].push(ticketGroups[i]);
    
             if ((i + 1) % 3 === 0) {
                 inc += 1;
             }
         }
    
         for (var i = 0, max = ticketTables.length; i < max; i += 1) {
             var table = ticketTables[i];
             for (var j = 0, jmax = table.length; j < jmax; j += 1) {
                 var row = table[j];
                 var newRow = [];
                 for (var k = 0, kmax = 9; k < kmax; k += 1) {
                     newRow[k] = '';
                     for (var l = 0, lmax = row.length; l < lmax; l += 1) {
                         var number = parseInt(row[l]);
    
                         if (number >= (k) * 10 && number < (k + 1) * 10) {
                             newRow[k] = number;
                         }
    
                         if (number >= (kmax * 10) && k === kmax - 1) {
                             newRow[k] = number
                         }
                     }
    
                 }
                 ticketTables[i][j] = newRow;
    
             }
         }
    
         console.log(ticketTables);
    
         for (var i = 0, max = ticketTables.length; i < max; i += 1) {
             var table = ticketTables[i];
             var $table = document.createElement('table');
             for (var j = 0, jmax = ticketTables[i].length; j < jmax; j += 1) {
                 var row = table[j];
                 var $row = document.createElement('tr');
                 $table.appendChild($row);
    
                 for (var k = 0, kmax = row.length; k < kmax; k += 1) {
                     var cell = row[k];
                     var $cell = document.createElement('td');
                     $row.appendChild($cell);
                     $cell.innerHTML = cell;
                 }
    
             }
             document.body.appendChild($table);
         }
     };
    
     loadTickets();
    input {
        width: 100%;
    }
    table, tr, td {
        border: 1px solid grey;
    }
    table tr td {
        min-width: 20px;
    }