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 8430477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:36:11+00:00 2026-06-10T05:36:11+00:00

I am probably overlooking something or just being a noob but, I am having

  • 0

I am probably overlooking something or just being a noob but, I am having trouble being able to pass key-value pairs to a javascript function.

I am writing a filter module utilizing a Backbone.View and Backbone.Paginator.clientPager collection. I added a function to the clientPager called whereExpanded() which searches the entire collection without modifying/rendering anything.

Backbone.Paginator.clientPager.prototype.whereExpanded = function(attrs) {
  return _.filter(this.origModels, function(model) {
    var key;
    for (key in attrs) {
      if (attrs[key] !== model.get(key)) {
        return false;
      }
  });
};

As you can see, it takes the attrs variable and loops over it. I want to be able to pass in key value pairs to this but, the key and value need to be dynamic.

collection.whereExpanded(filterByField, filterByValue)

filterByField is the attribute of the Backbone.Model I wish to filter by and filterByValue is the value of the attribute I want to filter. I have tried utilizing eval() but I cannot seem to get that to work either.

Any help woudl be greatly appreciated!

  • 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-10T05:36:12+00:00Added an answer on June 10, 2026 at 5:36 am

    You don’t need eval for this, you can build an object in better ways:

    var attrs = { };
    attrs[filterByField] = filterByValue;
    

    And with a small bit of effort, you can let your function be called in various different ways:

    whereExpanded({ k1: v1, k2: v2 });
    whereExpanded('k1', v1);
    whereExpanded('k1', v1, 'k2', v2);
    

    You just need to parse arguments yourself with something like this:

    argv  = [].slice.call(arguments);
    attrs = {};
    if(argv.length == 1 && _(argv[0]).isObject()) {
        attrs = argv[0];
    }
    else if(argv.length % 2 == 0) {
        for(var i = 0; i < argv.length; i += 2) {
            attrs[argv[i]] = argv[i + 1];
        }
    }
    else {
        throw 'Bad argument list';
    }
    

    That will leave you with the key/value pairs in attrs that your _.filter is expecting.

    Argument parsing demo: http://jsfiddle.net/ambiguous/e5kkc/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably I'm being stupid and overlooking something, but I am having a bizarre issue
Probably something really simple, but I'm having some real trouble with it. I have
I've got a problem here that's probably something that I'm just overlooking, but I
I'm probably just overlooking something obvious, but I can't for the life of me
I'm probably overlooking something that will be obvious when I see the solution, but
I'm probably overlooking something, but I got a class parse with a function getAllElements.
All, This question probably has a very simple answer - something I'm overlooking. But
This is probably something simple I'm overlooking but my google-fu isn't turning anything up
This is probably stupid simple, but for some reason I'm having trouble getting it
Hi everyone this is probably something extremely simple that i'm overlooking but can someone

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.