Last active
February 11, 2016 21:17
-
-
Save robmint/5f7cd7664aa38b67312e to your computer and use it in GitHub Desktop.
Split an array into roughly even sized columns - index style. You could use flex-box css to do this on the client side.
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
| // format an array into columns as sub arrays | |
| var splitter = function (columns, data) { | |
| try { | |
| var out = [], col = []; | |
| var colSize = Math.ceil(data.length / columns); | |
| var count = 0; | |
| for (key in data) { | |
| if (count > colSize) { | |
| out.push(col); | |
| col = []; | |
| count = 0; | |
| } | |
| var item = data[key]; | |
| col.push(item); | |
| count++ | |
| } | |
| if (col.length < columns) { | |
| out.push(col); | |
| } | |
| } catch (e) { | |
| cl({splitter: e}) | |
| } | |
| return out; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment