猿问

获取循环遍历 HTML 表的数组数组

我有这一堆代码


var rows = document.querySelectorAll('.workRow')

var codes = []

for(var i=0;i<rows.length;i++){

    var timeCodesInputs = rows[i].getElementsByClassName('xCell')

    for(var j=0;j<timeCodesInputs.length;j++){

        if(timeCodesInputs[j].innerHTML == "x"){

            codes.push(timeCodesInputs[j].dataset.dataHour)

        }

    }

}

它工作正常,但它把所有东西都推到一个数组中。我想要得到一个数组数组,其中一个数组如果来自一行的数据。怎么做?


慕后森
浏览 199回答 1
1回答

函数式编程

再见,尝试将行数据推送到一个数组中,然后将此数组推送到codes数组中,例如:var rows = document.querySelectorAll('.workRow')&nbsp; var codes = []&nbsp; for(var i=0;i<rows.length;i++){&nbsp; &nbsp; &nbsp; var rowArray = [];&nbsp; &nbsp; &nbsp; var timeCodesInputs =&nbsp; rows[i].getElementsByClassName('xCell')&nbsp; &nbsp; &nbsp; for (var j=0;j<timeCodesInputs.length;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(timeCodesInputs[j].innerHTML == "x"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowArray.push(timeCodesInputs[j].dataset.dataHour)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; codes.push(rowArray)&nbsp; &nbsp;}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答