Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:24:36+00:00 2026-06-06T17:24:36+00:00

Is this a good approach to filtering a backbone collection on multiple attributes: filterData:

  • 0

Is this a good approach to filtering a backbone collection on multiple attributes:

filterData: function(params) {
    // store original data for clearing filters
    this.originalModels = this.models.slice();
    for (var key in params) {
        var val = params[key];
        // if we are dealing with multiple values in an array
        // i.e. ['ford','bmw','mazda']
        if (typeof val === "object") {
            var union = [];
            for (var k in val) {
                var subval = val[k];
                var matched = _.filter(this.models, function(house) {
                   return house.get(key) == subval; 
                });
                union = union.concat(matched);
            }
            this.models = union;
        } else {
           var results = _.filter(this.models, function(house) {
               return house.get(key) == val;
           });
           this.models = results;
        }
    } // end for
    return this.reset(this.models);
},
clearFilters: function() {
    return this.reset(this.originalModels);
}

I tested it and it allows me to filter a collection in the following manner:

filterParams = {brand:['ford','bmw','mazda'], color:black}

carCollection.filterData(filterParams);

It seems to work, but I don’t know if there is a better way of doing this.

I tested the Backbone.Collection.where() method, but it does not work if I want to say brand: ['bmw','mazda','ford']

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:24:39+00:00Added an answer on June 6, 2026 at 5:24 pm

    You should be able to use something like this:

    filterData: function(params) {
        // store original data for clearing filters
        this.originalModels = this.models.slice();
    
        _.each(params, function(val, key){
            if (typeof val !== 'object') val = [ val ];
            this.models = _.filter(this.models, function(model){
                return _.indexOf(val, model.get(key)) !== -1;
            }, this);
        }, this);
        return this.reset(this.models);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this good approach to get data from stored procedure? For example procedure is
I'm new to RabbitMQ and was wondering of a good approach to this problem
I am new to Protocol Buffers and seeing this as good approach to go.
is this good code? can it be simplified somehow? SELECT u.id,u.title,u.title,u.first,u.last FROM (((tblusers u
Is this good OO Design assuming you want every inheriting class to be a
Is this good practice? Storing objects accessed by a constant. There are plenty of
I read this good article about Routed Events, and I understood why we need
i want to integrate twitter into my iphone application so i read this good
Is this a good practise,or it just depends? Thanks.
Is this a good method to save to all index in a variable and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.