Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added _.groupBy #195

Merged
merged 3 commits into from
May 5, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ <h2>Table of Contents</h2>
<a href="#any">any</a>, <a href="#include">include</a>,
<a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
<a href="#max">max</a>, <a href="#min">min</a>,
<a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>,
<a href="#sortBy">sortBy</a>, <a href="#groupBy">groupBy</a>, <a href="#sortedIndex">sortedIndex</a>,
<a href="#toArray">toArray</a>, <a href="#size">size</a></span>
</p>

Expand Down Expand Up @@ -437,6 +437,17 @@ <h2>Collection Functions (Arrays or Objects)</h2>
<pre>
_.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
=&gt; [5, 4, 6, 3, 1, 2]
</pre>

<p id="groupBy">
<b class="header">groupBy</b><code>_.groupBy(list, iterator)</code>
<br />
Splits a collection into sets, grouped by the result of running each
value through <b>iterator</b>.
</p>
<pre>
_.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
=&gt; {1: [1.3], 2: [2.1, 2.4]}
</pre>

<p id="sortedIndex">
Expand Down
6 changes: 6 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ $(document).ready(function() {
people = _.sortBy(people, function(person){ return person.age; });
equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');
});

test('collections: groupBy', function() {
var parity = _.groupBy([1, 2, 3, 4, 5, 6], function(num){ return num % 2; });
equals(_.keys(parity).join(', '), '0, 1', 'created a group for each value');
equals(parity[0].join(', '), '2, 4, 6', 'put each even number in the right group');
})

test('collections: sortedIndex', function() {
var numbers = [10, 20, 30, 40, 50], num = 35;
Expand Down
10 changes: 10 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@
return a < b ? -1 : a > b ? 1 : 0;
}), 'value');
};

// Groups the object's values by a criterion produced by an iterator
_.groupBy = function(obj, iterator) {
var result = {};
each(obj, function(value, index) {
var key = iterator(value, index);
(result[key] || (result[key] = [])).push(value)
});
return result;
}

// Use a comparator function to figure out at what index an object should
// be inserted so as to maintain order. Uses binary search.
Expand Down