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

  • Home
  • SEARCH
  • 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 6340241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:47:52+00:00 2026-05-24T19:47:52+00:00

I currently have some code to filter an array based on price range using

  • 0

I currently have some code to filter an array based on price range using a slider. I need to be able to add checkboxes for various sizes and colors so that I can also filter using their values. This is the code I have so far but am unsure how to implement checkboxes so that I have have multiple ways of filtering the array.

//this is the main generated array
    var product = [{"price":"200","color":"red","size":"small"},
                   {"price":"250","color":"brown","size":"medium"},
                   {"price":"300","color":"red","size":"large"}];

// array to display filtered array
    var filteredProducts = [];  
    var key = 0;

//start of price range filter
    if(!minPrice && !maxPrice){
       filteredProducts = products;
    } else{
       $.each(products, function(i, object) {   
           var curentPrice = parseFloat(object.price); 
           var priceMinOk = true;
           var priceMaxOk = true;
       // filter results match the price range
           if(maxPrice || minPrice){
              if(maxPrice && maxPrice<curentPrice){
                 priceMaxOk = false;
              }
              if(minPrice && minPrice>curentPrice){
                 priceMinOk = false;
              }
           }  
       //  loop over list and get only related to new array
           if( priceMinOk && priceMaxOk ){  
              filteredProducts[key] = object;                  
              key++;
           }  
       });
    } 

Thanks in advance for any help”

Fiddle http://jsfiddle.net/ltbmedia/86pEn/

  • 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-24T19:47:52+00:00Added an answer on May 24, 2026 at 7:47 pm

    Use $.grep instead of $.each, and structure the code like this:

    var products = [ /* ... */ ],
        predicates = [
            function predicate1(obj) { /* ... */ },
            function predicate2(obj) { /* ... */ },
            // ... ,
            function predicateN(obj) { /* ... */ }
        ],
        filteredProducts;
    
    filteredProducts = $.grep(products, function (element, index)
    {
        for (var i=0; i<predicates.length; i++)
        {
            if (!predicates[i](element)) return false;
        }
    
        return true;
    });
    

    Example: http://jsfiddle.net/mattball/Rsbcu/


    More complex example: http://jsfiddle.net/mattball/vZzjM/

    You might notice that you’re still getting an empty array back, but this actually makes sense. Given the criteria you’ve specified (minPrice = 201, maxPrice = 301, color = red or green, size = small) there are no matching array elements.

    Loosen up the price criteria just a tiny bit and you’ll see that everything works as expected: http://jsfiddle.net/mattball/MQ8Mc/

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

Sidebar

Related Questions

I'm using Zend_Search_Lucene, the PHP port of Java Lucene. I currently have some code
I currently have some code that will produce a crash dump when my application
I currently have some code that pulls down a list of users in a
I've been trying to implement unit testing and currently have some code that does
Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter
Currently, I have some basic code to play a simple tone whenever a button
I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
I'm not sure if I asked the question correctly. I have some code I
I have some Python code that works correctly when I use python.exe to run
We have a scenario where some .NET code is attempting to access the current

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.