Created
March 3, 2014 03:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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