Skip to content

Instantly share code, notes, and snippets.

@cedrickvstheworld
Created March 5, 2020 08:03
Show Gist options
  • Save cedrickvstheworld/98ed29b17dd97d2acf4e6836f655a387 to your computer and use it in GitHub Desktop.
Save cedrickvstheworld/98ed29b17dd97d2acf4e6836f655a387 to your computer and use it in GitHub Desktop.
let testArr = [...Array(23).keys()]
function chunk(arr, size) {
let chunkie = []
let newConstruct = []
for (let i in arr) {
if (chunkie.length < size - 1) {
chunkie.push(arr[i])
if (arr.length === parseInt(i) + 1) {
newConstruct.push(chunkie)
}
}
else {
chunkie.push(arr[i])
newConstruct.push(chunkie)
chunkie = []
}
}
return newConstruct
}
console.log(chunk(testArr, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment