0

I have an array which I would like to break into 4 pieces (4 arrays), and then store those 4 arrays into another array.

This is what I have so far:

a=[1,2,3,4,5,6,7,8,9,10,11,12]

while(a.length) {
     a.splice(0,3); 
}
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 2
    possible duplicate of [Split array into chunks](http://stackoverflow.com/questions/8495687/split-array-into-chunks) – Blauharley May 16 '15 at 21:48

1 Answers1

2

That's a good start. You also need an array to put the arrays in:

var a = [1,2,3,4,5,6,7,8,9,10,11,12];
var result = [];

while (a.length > 0) {
  result.push(a.splice(0,3));
}

(Using while (a.length) works fine, but I like to use the more specific condition while (a.length > 0).)

Guffa
  • 687,336
  • 108
  • 737
  • 1,005