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);
}
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);
}
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)
.)