Created
March 30, 2019 07:45
-
-
Save lh0x00/52371593dc5a411fc9e133e5b09d4f03 to your computer and use it in GitHub Desktop.
This file contains 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
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