javascript array split into a set of three

First build the original data.

var arr = [
  {name:'yanggb1',age:'15'},
  {name:'yanggb2',age:'16'},
  {name:'yanggb3',age:'17'},
  {name:'yanggb4',age:'18'},
  {name:'yanggb5',age:'19'},
  {name:'yanggb6',age:'20'},
  {name:'yanggb7',age:'21'},
  {name:'yanggb8',age:'22'}
];

The array is then grouped three three.

var arr3 = [];
for(var i = 0; i< arr.length; i += 3){
    arr3.push(arr.slice(i, i + 3));
}

Finally, you can see the result is a two-dimensional array.

This is by the slice () method is convenient, manner if the modulo index to more complex.

 

"If you work just because of boredom only to find something to do, rather than for just a meal. That would be great."

Guess you like

Origin www.cnblogs.com/yanggb/p/11993979.html