Created
March 28, 2024 02:54
-
-
Save aliboy08/5ed1ed9933cb9eb10899f3f73db08778 to your computer and use it in GitHub Desktop.
group array of items into multiple sub arrays
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 group_items(items, items_per_group){ | |
var groups = [ | |
[], | |
]; | |
var current_group = 0; | |
var group_items_count = 0; | |
for( var i = 0; i < items.length; i++ ) { | |
groups[current_group].push(items[i]); | |
group_items_count++; | |
var is_last = i+1 == items.length; | |
if( group_items_count == items_per_group && !is_last ) { | |
// new group | |
groups.push([]); | |
current_group++; | |
group_items_count = 0; | |
} | |
} | |
return groups; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment