Skip to content

Instantly share code, notes, and snippets.

@robmint
Last active February 11, 2016 21:17
Show Gist options
  • Select an option

  • Save robmint/5f7cd7664aa38b67312e to your computer and use it in GitHub Desktop.

Select an option

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.
// 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