In fact, all underscore counting methods are proxied to Backbone.Collection objects. when you execute collection.map(... , it returns an array of objects returned by the displayed function. The solution provided by raina77ow does not work, because Backbone.Collection is not an array, and assigning the result of the this.collection map this.collection destroy the collection itself.
If you want to filter a collection, I would recommend using the filter method. (I assume that you are working with Backbone.View :
var filter = this.$('#search-field').val(), filteredModels = this.collection.filter( function( model ) { return model.get('name').toLowerCase() === filter; }; this.collection.reset( filteredModels );
Note that any of the proxied underline methods in collections returns an array of models. If you then want to use them, you can reset the collection with these models or, equivalently, set the attribute of the collection models in the filtered results: this.collection.models = filteredModels . The first form has the advantage of triggering a reset event in a collection that you can listen to and, for example, redisplay your view.
source share