Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert simple array into two-dimensional array(matrix) in javascript
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;
}
//listToMatrix(array , 2);
@premsh

This comment has been minimized.

Copy link

@premsh premsh commented Feb 18, 2016

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment