Skip to content

Instantly share code, notes, and snippets.

@lh0x00
Created March 30, 2019 07:45
Show Gist options
  • Save lh0x00/52371593dc5a411fc9e133e5b09d4f03 to your computer and use it in GitHub Desktop.
Save lh0x00/52371593dc5a411fc9e133e5b09d4f03 to your computer and use it in GitHub Desktop.
function splitArray(arr, k) {
const length = arr.length;
const middle = Math.floor(length / k);
const left = arr.slice(middle * k, length);
let result = [], cursor = 0;
while(cursor < middle) {
result.push(arr.slice(cursor * k, cursor * k + k))
cursor++;
}
if (left.length === 0) {
return result;
}
return result.concat([left]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment