判断每一行下面的列的个数是否相同

我有一个文本域里面粘贴了很多用tab按键分割的矩阵,如何判断每一行下面的列是否相同呢,求解,最好给出简单代码参考一下,谢谢


开满天机
浏览 445回答 1
1回答

白板的微信

var text = "1&nbsp; &nbsp; 2&nbsp; &nbsp; 3&nbsp; &nbsp; 4\n2&nbsp; &nbsp; 3&nbsp; &nbsp; 33"function checkText(text) {&nbsp; &nbsp; text = text.split("\n") //按行分割成数组&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; let oLen = 0&nbsp; &nbsp; for (let k = 0; k < text.length; k ++) {&nbsp; &nbsp; &nbsp; &nbsp;let item = text[k].split("\t") //按tab分割成数组&nbsp; &nbsp; &nbsp; &nbsp;let nLen = item.length //每行列数量&nbsp; &nbsp; &nbsp; &nbsp;if (!oLen) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oLen = nLen&nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (oLen !== nLen) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log('有列不相等')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oLen = nLen&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; console.log('全部相等')}checkText(text)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript