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

小程序交流群:609690978

方法一:

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<data.length;i+=3){ 
    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]]

小程序交流群:609690978

你可能感兴趣的:(数组的操作)