js實現把多個數據分紅3個一組

主要代碼:html

var stuCount  = res.data;
 
   
resetDataFun(stuCount,5)
/**
* col 表示列
* stuCount 表示數組
*/
 
   
function resetDataFun(stuCount,col){ if (stuCount.length>0){ var objList = new Object(); var arr = new Array(); var cow = stuCount.length / col == 0 ? stuCount.length / col : Math.ceil((stuCount.length / col)); console.log('行數',cow); for (var i = 1; i <= cow; i++) { var tempArr = []; var temp; if (col * i > stuCount.length) { temp = stuCount.length; } else { temp = col * i; } for (var j = col * (i - 1); j < temp; j++) { tempArr.push(stuCount[j]); } var newObj = new Object(); newObj.data = tempArr arr.push(newObj); } objList.data = arr; this.resetData = objList; console.log(objList); } }

 

  獲得的數據爲三個一組數組

相關文章
相關標籤/搜索