Skip to content

Instantly share code, notes, and snippets.

@aliboy08
Created August 13, 2024 04:24
Show Gist options
  • Save aliboy08/30c5f3459f937662f58e3e1fd716b4e9 to your computer and use it in GitHub Desktop.
Save aliboy08/30c5f3459f937662f58e3e1fd716b4e9 to your computer and use it in GitHub Desktop.
Batch items
export function batch_items(items, items_per_batch){
let batched_items = [];
let group_items = [];
let group_index = 0;
for( let i = 0; i < items.length; i++ ) {
group_index++;
group_items.push(items[i]);
if( group_index == items_per_batch || i == items.length-1 ) {
// new group / reset
batched_items.push(group_items);
group_items = [];
group_index = 0;
}
}
return batched_items
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment