如何将forEach转换为For循环

时间:2017-08-11 08:04:14

标签: jquery for-loop foreach

将forEach转换为Forloop的正确方法是什么?这是我的示例代码。

$.get('/files/foods.txt', function (data) {
    var build = "";
    build += "<tr>";
    build += "<th>Pizza</th>";
    build += "<th>Ice cream</th>";
    build += "</tr>";
    build += "<tr>\n";

    var rows = data.split("\n");
    rows.forEach(function getvalues(thisRow) {
        var foodArray = thisRow.split(",");
        for (var i = 0; i < foodArray.length; i++) {
            build += "<td class=''>" + foodArray[i] + "</td>";
        }
        build += "</tr>\n";
    })

    display.innerHTML = build;
});

1 个答案:

答案 0 :(得分:1)

您可以使用解决方案

$.get('/files/foods.txt', function (data) {
    var build = "";
    build += "<tr>";
    build += "<th>Pizza</th>";
    build += "<th>Ice cream</th>";
    build += "</tr>";
    

    var rows = data.split("\n");
    for (var i = 0; i < rows.length; i++){ 
       build += "<tr>";
       var foodArray = rows[i].split(",");
       for(var j=0;j<foodArray.length;j++){
          build += "<td class=''>" + foodArray[j] + "</td>";
       }
       build += "</tr>";
    }

    display.innerHTML = build;
});