Created
May 17, 2015 15:08
-
-
Save maxnachlinger/462f3cfbd0f360971283 to your computer and use it in GitHub Desktop.
1d array to 2d array of columns
This file contains hidden or 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
/* | |
Given an array [0,1,2,3,4] and 3 columns, create: | |
[ [0,3], [1,4], [2] ] to power a screen that looks like: 0 | 1 | 2 | |
3 | 4 | |
*/ | |
var a = []; | |
for(var i = 0; i < 5; i++) { | |
a[i] = i; | |
} | |
var amtCols = 3; | |
var colIdx; | |
var columns = []; | |
for(var i = 0, c = a.length; i < c; i++) { | |
colIdx = i % amtCols; | |
if(!columns[colIdx]) | |
columns[colIdx] = []; | |
columns[colIdx].push(a[i]); | |
} | |
console.log(columns); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment