Limit underscorejs group by length

637 Views Asked by At

I have a long list of articles which I grouped by category, how can I limit it so each category will have only 4 (or any other number) items?

My input is: enter image description here

and I want only 4 articles on each section

1

There are 1 best solutions below

2
On BEST ANSWER

Map the object values to sliced versions of themselves:

_.mapObject(groupedArticles, function(group) { return group.slice(0, 4); })