Skip to content

Instantly share code, notes, and snippets.

@ninadvadujkar
Created September 18, 2019 12:08
Show Gist options
  • Save ninadvadujkar/3d8cd2325667f573ea83954aec919101 to your computer and use it in GitHub Desktop.
Save ninadvadujkar/3d8cd2325667f573ea83954aec919101 to your computer and use it in GitHub Desktop.
function explode(array, chunkSize) {
if (chunkSize === 0) return [];
const explodedArr = [];
let innerArr = [];
for (let i = 0; i < array.length; i++) {
innerArr.push(array[i]);
if ( (i + 1) % chunkSize === 0) {
explodedArr.push(innerArr);
innerArr = [];
}
}
if (innerArr.length !== 0) {
explodedArr.push(innerArr);
}
return explodedArr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment