Skip to content

Instantly share code, notes, and snippets.

@amboy00
Created March 24, 2014 09:23
Show Gist options
  • Save amboy00/9737014 to your computer and use it in GitHub Desktop.
Save amboy00/9737014 to your computer and use it in GitHub Desktop.
function listToMatrix(list, elementsPerSubArray) {
var matrix = [], i, k;
for (i = 0, k = -1; i < list.length; i++) {
if (i % elementsPerSubArray === 0) {
k++;
matrix[k] = [];
}
matrix[k].push(list[i]);
}
return matrix;
}
var matrix = listToMatrix([1, 2, 3, 4, 4, 5, 6, 7, 8, 9], 3);
// result: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment