Skip to content


Add `difference` underscore.js method to Backbone.Collection #2491

wants to merge 4 commits into from

3 participants


Adds the difference underscore.js method to Collection, allowing for this

var arr = _.difference(myCollection.models, excludeThese)

to become this

var arr = myCollection.difference(excludeThese)

I added a test for it as well, which passes. However, when I forked off master, a failing test already existed.


@mikefrey thanks for opening a pull request.

What's the real world use case behind this? I'm curious because it doesn't seem like all that common of a pattern...


Hi @wookiehangover
Thanks for the quick response!

My primary use-case is presenting items to add from one group to another, excluding the items in first group that are already in the second (adding users into groups for example). That being said, I can see difference having the same use-cases as without, which is already included in Collection.


@mikefrey I know it's been a little while since you opened this, but would it be possible for you to push a version of this with that merges cleanly with master?


@wookiehangover I've updated my branch, but it looks like difference was already added to master. I suppose you could still merge this for the tests.


@mikefrey we actually already have a test for difference as well, but thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 2 additions and 1 deletion.
  1. +2 −1 test/collection.js
3 test/collection.js
@@ -503,7 +503,7 @@ $(document).ready(function() {
equal(coll.findWhere({a: 4}), void 0);
- test("Underscore methods", 14, function() {
+ test("Underscore methods", 15, function() {
equal({ return model.get('label'); }).join(' '), 'a b c d');
equal(col.any(function(model){ return === 100; }), false);
equal(col.any(function(model){ return === 0; }), true);
@@ -514,6 +514,7 @@ $(document).ready(function() {
ok(_.include(, d));
ok(!_.include(col.without(d), d));
+ ok(!_.include(col.difference([d]), d));
equal(col.max(function(model){ return; }).id, 3);
equal(col.min(function(model){ return; }).id, 0);
Something went wrong with that request. Please try again.