Skip to content

Instantly share code, notes, and snippets.

@Layoric
Created March 3, 2014 03:56
Show Gist options
  • Save Layoric/9318130 to your computer and use it in GitHub Desktop.
Save Layoric/9318130 to your computer and use it in GitHub Desktop.
Javascript function to split a flat array into a fixed number of columns
var arrayToTable = function (array, numOfCols) {
var arrayLength = array.length;
var rows = [{
columns: []
}];
var rowNum = 0;
for(var i = 0; i < arrayLength; i++) {
var rowMod = (i) % numOfCols;
if(rowMod === 0) {
rows.push({
columns: []
});
}
rows[rowNum].columns.push(array[i]);
if(rowMod === numOfCols - 1) {
rowNum++;
}
}
return rows;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment