Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda