在开发过程中,我们有时会遇到后端返回的是一维数组,而我们需要的是按指定个数分割的二维数组的情况,如果你对此还没有思路,赶快花两分钟get一下新技能吧!
  var data = [
   {name:'Liming',age:'25'},
   {name:'Liming',age:'25'},
   {name:'Liming',age:'25'},
   {name:'Liming',age:'25'},
   {name:'Liming',age:'25'},
   {name:'Liming',age:'25'},
   {name:'Liming',age:'25'},
   {name:'Liming',age:'25'},
   {name:'Liming',age:'25'},
   {name:'Liming',age:'25'},
 ]

   方法一:
    var result = [];
    for(var i=0;i<data.length;i+=3){
        result.push(data.slice(i,i+3));
    }

方法二:
var result=[];
   var dataArr=[...data];
   for(let i=0,j=0;i<data.length;i+=8;j++){
     result[j]=dataArr.splice(0,8);
  }
  第二种方法,每次循环有两个变量,i和j,每当i+8时,j++。

冰冰冰柠檬
33 声望1 粉丝