js实现把多个数据分成3个一组


主要代码:

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);
                }
            }

 

  得到的数据为三个一组


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM