JS将某个数组分割为N个对象一组(如,两两一组,三三一组等)

方法一:

var result = [];

var data = [

  {name:'chen',age:'25'},

  {name:'chen',age:'25'},

  {name:'chen',age:'25'},

  {name:'chen',age:'25'},

  {name:'chen',age:'25'},

  {name:'chen',age:'25'},

  {name:'chen',age:'25'},

  {name:'chen',age:'25'},

  {name:'chen',age:'25'},

  {name:'chen',age:'25'},

]

for(var i=0;i

    result.push(data.slice(i,i+3));

}

  //[

//[{name:'chen',age:'25'},{name:'chen',age:'25'},{name:'chen',age:'25'}],

//[{name:'chen',age:'25'},{name:'chen',age:'25'},{name:'chen',age:'25'}],

//[{name:'chen',age:'25'},{name:'chen',age:'25'},{name:'chen',age:'25'}],

//[{name:'chen',age:'25'}],

  //]

方法二:

var spArray = function(N,Q){

var R = [],F;

for (F = 0;F < Q.length;) {

R.push(Q.slice(F,F += N))

}

return R

}

var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];

console.log(spArray(5,arr))//[[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14]]

————————————————

版权声明:本文为CSDN博主「The wind is blowing」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_43156398/article/details/90715915

**JS将某个数组分割为N个对象一组(如,两两一组,三三一组等)**_Java_你见过思念放过谁的博客-CSDN博客

你可能感兴趣的:(JS将某个数组分割为N个对象一组(如,两两一组,三三一组等))