Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
How can I create a JavaScript filter function for multiple filters at once

My Problem:

I'm having a website where I can compare products stored inside an array (with objects). I want to add different filters from array inside of an object that get applied together.

For two filters I can easily do it (see my code below). I just compare two objects and use a filter depending on their content.

But what would be a good approach to use the filter if there are more than two objects. Can I loop through the object and compare if the arrays are empty?

With my current approach I would have to extend my code for every new filter and it would balloon.

What I'm trying to do:

I want to check which filter objects have any data in their "feature" array (that array gets filled after the user clicks a filter on the site) and if they have I want to use these arrays to filter the main filteredArray array.

My current Object:

features_collection: {
    aspect_ratio_object: {
      features: [],
      value: "Aspect Ratio",
    },
    performance_rating_object: {
      features: [],
      value: "Performance Rating",
    },
  },

My Filter Function:

    if (
      features_collection.aspect_ratio_object.features.length &&
      features_collection.performance_rating_object.features.length
    ) {
      return filteredArray.filter(
        (obj) =>
          features_collection.aspect_ratio_object.features.includes(
            obj[features_collection.aspect_ratio_object.value]
          ) &&
          features_collection.performance_rating_object.features.includes(
            obj[features_collection.performance_rating_object.value]
          )
      );
    } else if (
      features_collection.aspect_ratio_object.features.length ||
      features_collection.performance_rating_object.features.length
    ) {
      return filteredArray.filter(
        (obj) =>
          features_collection.aspect_ratio_object.features.includes(
            obj[features_collection.aspect_ratio_object.value]
          ) ||
          features_collection.performance_rating_object.features.includes(
            obj[features_collection.performance_rating_object.value]
          )
      );
    }
  },

Further Notes:

I can also change my object. I could change it into an array of objects if that would make things easier?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You obviously have too loop through your object.

Here is your loop code for features_collection:

features_collection.forEach(function (item, index) {
  console.log(item, index);
});
about 4 years ago · Juan Pablo Isaza Report

0

Making your filters an array seems more practical. Here's an example on how to filter a set of objects against your feature_collection.

function filter_by_features(targets, feature_collection) {
    
    // Start right of to filter the `filteredArray`
    return targets.filter(obj => {

        // go through every feature and test it against the current object.
        // every() returns either true or false and the targets array is filtered 
        // by that condition supplied within the callback of `every()`
        return feature_collection.every(filter => {

            // If for a given feature no filter is available, return true
            // so the test for this filter passes.
            if(filter.features.length === 0) {
                return true
            }

            // there are features, check if any applies.
            return filter.features.includes(obj[filter.value])
    
        })

    })

}

Usage

// feature collection  (as array)
const feature_collection = [
    {
        features: [],
        value: "Aspect Ratio",
    },
    {
        features: [],
        value: "Performance Rating",
    }
]

// the objects you want to filter.
const objects_to_filter = [/* ... */]

const filtered = filter_by_features(objects_to_filter, feature_collection)

docs

  • every()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!