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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:25:33+00:00 2026-05-27T23:25:33+00:00

I try to use in my app, simple comparator to filter some data with

  • 0

I try to use in my app, simple comparator to filter some data with passing string filter instead function as eg. passed to [].filter
Comparator should return function which will be a filter.

   var comparator = function( a, b, c ) { 
        switch( b ){
            case '>=': return function() { return this[a] >= c;}; break;
            case '<=': return function() { return this[a] <= c;}; break;
            case '<':  return function() { return this[a] < c;}; break;
            case '>':  return function() { return this[a] > c;}; break;
            case '=':  return function() { return this[a] == c;}; break;
            case '==': return function() { return this[a] === c;}; break;
            case '!=': return function() { return this[a] != c;}; break;
            default: return null;
        };

    }

Assume that i get this function by:

  var filterFn = comparator.apply({}, /(.+)(=|>=|<=|<|>|!=|==|!==)(.+)/.exec( "id<4" ).slice(1) );


  someModel = someModel.objects.filter( filterFn );

The target it will look:

   someModel.get = function( filter ){ 
      return new Model(  
           this.objects.filter(
               comparator.apply({}, /(.+)(=|>=|<=|<|>|!=|==|!==)(.+)/.exec( "id<4" ).slice(1) 
           ) 
      );
   };
   var filtered = someModel.get( "id<4" );

Question is – I assume that it will be a lot more operators and I have no idea how to write it more simply.

Using Eval is out of question.

This code didn’t was both executed and tested I wrote it just to show what I mean.

  • 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-05-27T23:25:33+00:00Added an answer on May 27, 2026 at 11:25 pm

    Store every function in an object, either pre-defined, or dynamically.

    If you want to dyanmically create the set of functions, define the comparator object as shown below. I assumed that you did not extend the Object.prototype. If you did, operators.hasOwnProperty(property) has to be used within the first loop.

    // Run only once
    var funcs = {};   // Optionally, remove `funcs` and swap `funcs` with `operators`
    var operators = { // at the first loop.
        '>=': '>=',
        '<=': '<=',
        '<' :  '<',
        '>' :  '>',
        '=' : '==', //!!
        '==':'===', //!!
        '!=': '!='
    }; // Operators
    
    // Function constructor used only once, for construction
    for (var operator in operators) {
        funcs[operator] = Function('a', 'c',
                           'return function() {return this[a] ' + operator + ' c};');
    }
    
    // Run later
    var comparator = function(a, b, c) {
        return typeof funcs[b] === 'function' ? funcs[b](a, c) : null;
    };
    

    When comparator is invoked, the returned function looks like:

    function() {  return this[a] < c;   }// Where a, c are pre-determined.
    

    This method can be implemented in this way (demo at JSFiddle):

    // Assumed that funcs has been defined
    function implementComparator(set, key, operator, value) {
        var comparator, newset = [], i;
    
        if (typeof funcs[operator] === 'function') {
            comparator = funcs[operator](key, value);
        } else { //If the function does not exist...
            throw TypeError("Unrecognised operator");
        }
    
        // Walk through the whole set
        for (i = 0; i < set.length; i++) {
            //  Invoke the comparator, setting `this` to `set[i]`. If true, push item
            if (comparator.call(set[i])) {
                newset.push(set[i]);
            }
        }
        return newset;
    }
    var set = [ {meow: 5}, {meow: 3}, {meow: 4}, {meow: 0}, {meow: 9}]
    implementComparator( set , 'meow', '<=', 5);
    // equals: [ {meow: 5}, {meow: 3}, {meow: 4}, {meow: 0} ]
    

    For clarification, I constructed this answer, while keeping the following in mind:

    • The OP requests an simple, easily extensible method with an unknown/dynamic set of operators.
    • The code is based on the pseudo-code at the OP, without changing anything which could affect the intent of the OP. With some adjustments, this function can also be used for Array.prototype.filter or Array.prototype.sort.
    • eval (or Function) should not be used at every call to comparator
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a simple winform test app i'm using to try some Log4Net Dependency
I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
hello i try to use simple application in android but i have many problems
I'm making a simple test app that navigates a category tree. Whenever I try
I'm trying to write a simple app that should mute my mobile phone for
I develop a simple app, when i try to save an XMLDocument to a
I'm trying to use android.provider.Telephony.SMS_RECEIVED to catch incoming SMS's. I built a simple app,
Consider a simple app server / database server setup. You want to set some
I'm trying to use LogCat to debug a simple app that reads an XML
I have a simple Rails 3.1.rc6 app that I'm using to try to test

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.