Skip to content

Instantly share code, notes, and snippets.

@matthewsimo
Last active December 13, 2015 17:18
Show Gist options
  • Select an option

  • Save matthewsimo/4946562 to your computer and use it in GitHub Desktop.

Select an option

Save matthewsimo/4946562 to your computer and use it in GitHub Desktop.
Function to return array groups with each group consisting of a certain min size of elements of a matching selector
findGroups = function(selector, size) {
var groups = []; var group = [];
$(selector).each(function(i, v){
group.push(v);
if(group.length === size) {
groups.push(group);
group = [];
}
});
if(group.length !== 0)
groups.push(group);
return groups;
}
@matthewsimo

Copy link
Copy Markdown
Author

If you want to get groups of 3 for all items matching the '.article-grid .article' selector:

findGroups('.article-grid .article', 3);

If you were going to use this with matchH:

columns = 3;
$(findGroups('.article-grid .article', columns)).each(function(){ $(this).matchH(); })

Of course you'd want to set columns intelligently based on the breakpoint...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment