Created
August 25, 2017 17:57
-
-
Save jhsuZerion/0330ee30fbe74f4830d08477ff2b1000 to your computer and use it in GitHub Desktop.
Convert a subform to a multi-dimensional array specified by the second parameter
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
/** | |
* function returns subform as 2D array | |
* @param {object} r valid JSON object | |
* @param {int} n integer greater than 1 | |
* @return {object} 2D array where number of objects per row is equal to n | |
*/ | |
function subform_to_multi_array(r,n) { | |
if(typeof n !== 'number' || n !== Math.floor(n) || n <= 1 || n > r.length) return r; | |
var cols = []; | |
var count = 0; | |
var row = 0; | |
for(var i=0; i<r.length; i+=n) { | |
cols[row] = []; | |
for(var j=0; j<n; j++) { | |
if(count < r.length) { | |
cols[row].push(r[count]); | |
count++; | |
} | |
} | |
row++; | |
} | |
return cols; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Different version used for columns: