简体   繁体   English

Javascript - 循环遍历数组上的元素,然后是下一个元素

[英]Javascript - Looping through elements on the array, then next elements

I'd need to construct HTML table based arrays.我需要构建基于 HTML 表的数组。 I'm almost here but stuck in one point.我快到了,但卡在了一点上。 Let me try to explain:让我试着解释一下:

I already could construct the header of the table, with as many column as I need, OK我已经可以构建表的标题了,我需要多少列,好的

rowLength = 2 in my case, OK - > so I need to create two data rows rowLength = 2 在我的情况下,好的 - > 所以我需要创建两个数据行

notUniq = 8 the full number of ma elements notUniq = 8 ma 元素的全部数量

arrayLength = 4, basically that is the table header column number. arrayLength = 4,基本上就是表头列号。

Answers is an array, with 8 element: Adela Cervantsz,true,Dell,Good lapotp please,false,Barton Friesner,iOS please,IPad Answers是一个数组,有 8 个元素:Adela Cervantsz,true,Dell,Good lapotp please,false,Barton Friesner,iOS please,iPad

So part of my code:所以我的代码的一部分:

    for (var k = 0; k < rowLength; k++) {
            string += "<tr>";
            for (var j = 0; j < notUniq; j++) {
                string += "<td style=" + "width: 5%; font-size:70%" + ">" + Answers[j] + "</td>";
            }
            string += "</tr>";
        }

It produces me: Please click to check So it crates 2 rows, ok, but each row has 8 columns, not ok, I'd need just the first 4 element, then the 2nd 4 element.它产生了我:请点击检查所以它装了 2 行,好吧,但每行有 8 列,不行,我只需要第一个 4 个元素,然后是第 2 个 4 个元素。

If I modify the code, so if "for" with arrayLength 4 times, so the code is:如果我修改代码,那么如果“for”与arrayLength 4次,那么代码是:

    for (var k = 0; k < rowLength; k++) {
            string += "<tr>";
            for (var j = 0; j < arrayLength; j++) {
                string += "<td style=" + "width: 5%; font-size:70%" + ">" + Answers[j] + "</td>";
            }
            string += "</tr>";
        }  

Then I get 2 rows, OK, 4 columns, OK, but both row has the same values :( Fors sure I'd need 1st row is the first 4 elements, the 2nd row is the next 4 elements.. Please click here to check the 2nd result然后我得到 2 行,好的,4 列,好的,但是两行都有相同的值:( 当然我需要第一行是前 4 个元素,第二行是接下来的 4 个元素..请点击这里检查第二个结果

Could you help me on this?你能帮我解决这个问题吗? I gues I'd need modify / extend somehow the Answers[l] part, to get the first 4, then the next.我想我需要以某种方式修改/扩展 Answers[l] 部分,以获得前 4 个,然后是下一个。 Really appreciate any help!真的很感激任何帮助! Thanks!谢谢!

You must consider the row index in the Answers array.您必须考虑 Answers 数组中的行索引。 Each new row, the index must be increased by arrayLength.每个新行,索引必须增加arrayLength。 Answers[k*arrayLength + j] should do the trick Answers[k*arrayLength + j]应该可以解决问题

for (var k = 0; k < rowLength; k++) {
    string += "<tr>";
    for (var j = 0; j < arrayLength; j++) {
        string += "<td style=" + "width: 5%; font-size:70%" + ">" + Answers[k*arrayLength + j] + "</td>";
    }
    string += "</tr>";
} 

Try this below:试试这个:

var length = i = 0;
for (var k = 0; k < rowLength; k++) {
     length+=arrayLength;
     string += "<tr>";
     for (var j = i; j < length; j++) {
          string += "<td style=" + "width: 5%; font-size:70%" + ">" + Answers[j] + "</td>";
     }
     string += "</tr>";
     i+=arrayLength;   
 }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM