Convert table to array in JavaScript without using jQuery

后端 未结 4 1590
离开以前
离开以前 2020-12-15 10:02

I need to convert the table grid i created into an multidimensional array according to the content inside the table. Array would be in format like:

 var arra         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 11:03

    Run a for loop through the rows and the fields:

    var array = [];
    var table = document.querySelector("table tbody");
    var rows = table.children;
    for (var i = 0; i < rows.length; i++) {
        var fields = rows[i].children;
      var rowArray = [];
      for (var j = 0; j < fields.length; j++) {
        rowArray.push(fields[j].innerHTML);
      }
      array.push(rowArray);
    }
    console.log(array);
    

    JSfiddle.

提交回复
热议问题