How to split a long array into smaller arrays, with JavaScript How to split a long array into smaller arrays, with JavaScript arrays arrays

How to split a long array into smaller arrays, with JavaScript


Don't use jquery...use plain javascript

var a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];var b = a.splice(0,10);//a is now [11,12,13,14,15];//b is now [1,2,3,4,5,6,7,8,9,10];

You could loop this to get the behavior you want.

var a = YOUR_ARRAY;while(a.length) {    console.log(a.splice(0,10));}

This would give you 10 elements at a time...if you have say 15 elements, you would get 1-10, the 11-15 as you wanted.


var size = 10; var arrayOfArrays = [];for (var i=0; i<bigarray.length; i+=size) {     arrayOfArrays.push(bigarray.slice(i,i+size));}console.log(arrayOfArrays);

Unlike splice(), slice() is non-destructive to the original array.


Just loop over the array, splicing it until it's all consumed.

var a = ['a','b','c','d','e','f','g']  , chunkwhile (a.length > 0) {  chunk = a.splice(0,3)  console.log(chunk)}

output

[ 'a', 'b', 'c' ][ 'd', 'e', 'f' ][ 'g' ]