Skip to content

Instantly share code, notes, and snippets.

@xdmorgan
Created January 29, 2019 02:13
Show Gist options
  • Save xdmorgan/2e4add6c1397dafa716ab08daf66b810 to your computer and use it in GitHub Desktop.
Save xdmorgan/2e4add6c1397dafa716ab08daf66b810 to your computer and use it in GitHub Desktop.
Attempt to balance an array into even columns;
const balance = (data: any[], columns: number = 4) => {
const offset = data.length % columns ? 1 : 0
const height = (data.length - (data.length % columns)) / columns + offset
const unbalanced = [...data]
const balanced = []
while (unbalanced.length) balanced.push(unbalanced.splice(0, height))
return balanced
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment