0

I have a collection called `mainItems``

this.mainItems;

which contains 18 models. And also one more collection object which contains selected items:

this.seletedItems;

I need to filter the main collection object based on other collection.

I have tried the below approach:

    var that = this;
    var model = _.reject(that.filteredItems.models, function(model1){
        return _.filter(that.collection.models, function(model2) {
           return model1.id == model2.id;
        });
    });

but this approach is not working properly. Is it possible to filter the main items by avoiding second iteration ?

Please help!

2

2 Answers 2

2

You can use the Underscore's methods proxied by Backbone to simplify your filter.

For example, to list the models in mainItems without the models in selectedItems, you could use

// reject the models found in selectedItems
var filtered = mainItems.reject(function(m) {
    return selectedItems.get(m.id);
});

Note that Collection.get is a hash lookup, making this a Backbone equivalent to the answer pointed by @GruffBunny in the comments.

And a demo

var mainItems = new Backbone.Collection([
    {id: 1},
    {id: 2},
    {id: 3},
    {id: 4}
]);

var selectedItems = new Backbone.Collection([
    {id: 2}
]);

var keep = mainItems.reject(function(m) {
    return selectedItems.get(m.id);
});

console.log(_.pluck(keep, 'id'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

somehow backbone methods are not working and I have modified the script using underscore.js with _.reject and _.bind.
0

You can use difference to get all the models that are not selected.

var mainItems = [{ a: 1 }, { b: 2 }, { c: 3 }];
var selectedItems = mainItems.slice(1);

console.log(_.difference(mainItems, selectedItems));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

3 Comments

Difference won't work here as the arrays contain objects and not literals.
@GruffBunny That's true. My example was not good. It works with references also, though.
@GruffBunny It really depends on details that the OP has actually not provided in the question. In my apps, this solution would work because models are not ever duplicated on the client: if two collections contain the same model, it must be the same JavaScript object. If the OP's selectedItems is produced by taking a model from mainItems and adding it to selectedItems then comparing by reference works. If not, it does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.