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

131
Vistas
How to filter an array by two indvidual strings from an object?

I'm working on a project where I need to filter 13 items by two different select box values, and I'm getting stuck on persisting the filter.

I have two select boxes that I've selected like so:

let pickupLocation = document.querySelector("#pa_location");  //values are 'complete-set', 'neck', 'bridge'.

let pickupType = document.querySelector("#pa_type1"); // Values are 'soapbar', 'dogear', 'short'.

What's Working:

I'm initializing an object like so:

const activeFilters = {};

To populate the values like so:

//Persist the Complete Set / Single
pickupLocation.addEventListener("change", function () {
  if (pickupLocation.value === "complete-set") {
    activeFilters.location = "set";
  } else {
    activeFilters.location = "single";
  }
});

pickupType.addEventListener("change", function () {
  if (pickupType.value === "soapbar") {
    activeFilters.type = "soapbar";
  } else if (pickupType.value === "dogear") {
    activeFilters.type = "dogear";
  } else {
    activeFilters.type = "short";
  }
});

// Returns something like
// {location: single, type: dogear}

I'm trying to filter an array of input elements by their value. I have 13 inputs each with a value containing words like set, single, dogear, soapbar etc.

Where I'm stuck:

I have a filter function that I'm trying to filter the values of these inputs by two values of the activeFilters object:

const performFilter = (covers) => {
  let results;
  let filteredValues = Object.values(activeFilters);

  filteredValues.forEach((value) => {
    results = covers.filter((cover) => cover.value.indexOf(value) !== -1);
  });

  return results;
};

The problem is my function is returning only one of the two words. For instance, if the my activeFilters object is {location: set, type: dogear} the filtered results array contains only one of them. Where am I going wrong?

Edit:

This function returns all inputs that match one of the activeFilters, and I apologize if I wasn't clear above, but I'd like it to match ALL of the Active Filters. Is this possible with the function below?

const performFilter = (covers) => {
  let results = []; // initialise the array
  let filteredValues = Object.values(activeFilters);

  filteredValues.forEach((value) => {
    let res = covers.filter((cover) => cover.value.indexOf(value) !== -1);
    results.push(...res);
  });

  console.log(results);
};

CODEPEN:

Codepen!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const performFilter = (covers) => {
  let results = []; // initialise the array
  let filteredValues = Object.values(activeFilters);

  filteredValues.forEach((value) => {
    let res = covers.filter((cover) => cover.value.indexOf(value) !== -1);
    // push the value it find individually
     // you were overriding the previous value with result = filter()

    results.push(...res); 


  });

  return results;
};

// according to Edited question

const performFilter = (covers) => {
  let results = []; // initialise the array
  let filteredValues = Object.values(activeFilters);

   return covers.filter((cover) => filteredValues.every(value => cover.value.indexOf(value) !== -1));

};
about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm not sure if I understood clearly your question, so feel free to comment it.

First, I suggest you to filter your covers array and inside the filtering function iterate through your selected filters. This is because the filter function returns the array already filtered and so you don't need to assign it to a result variable or things like that. So based on that, try this:

const performFilter = (covers) => {
    let results;
    let filteredValues = Object.values(activeFilters);
    
    const filteredCovers = covers.filter((cover)  => {
        return cover.value.split("-").some((tag) => filteredValues.includes(tag))
    });
    
    console.log(filteredCovers)
};
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