Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda