Skip to content

Instantly share code, notes, and snippets.

@maxnachlinger
Created May 17, 2015 15:08
Show Gist options
  • Save maxnachlinger/462f3cfbd0f360971283 to your computer and use it in GitHub Desktop.
Save maxnachlinger/462f3cfbd0f360971283 to your computer and use it in GitHub Desktop.
1d array to 2d array of columns
/*
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