• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

124
Vistas
Cannot figure out multiple filters combined with search

so I've been staring at this thread for the past few days

javascript filter array multiple conditions

And while it offers fantastic solutions if know the filter params, what happens when I don't know them both? I have no clue how to do that.

I have a very basic filter setup going...

// check for text search values to populate names 
    this.searchBar.addEventListener("keyup", e => {
      this.searchString = e.target.value.toLowerCase();
      this.new_table();
    });

// check for gender options 
    this.genderFilter.addEventListener("change", e => {
      this.genderOption = e.target.value;
      this.new_table();
    });

// Build out the filter
  new_table() {
    const searchText = this.searchString;

    let combinedFilter = this.response.filter(char => {
      const name = char.first_name.includes(searchText) || char.last_name.includes(searchText);
      const gender = this.genderOption;

      return name || char.gender == this.genderOption;
    });

    this.build_table(combinedFilter);
  }

// build_table is my function that takes the json data and converts it into html

This works fantastic for the OR situation, but if I change the return to &&, it requires both to be filled out to show any results.

I'm pretty new with JS but I am thinking a few ways to do it and I'm probably wrong on all.

  1. Build out the search params and store them into an object (ie {firstname: name, gender: gender}) and then this way I can just leave blank if empty. I don't know how to actually return that, however. I tried a few different ways and I'm not even sure if that is possible.
  2. Figure out a default value for each which likely works well with the dropdowns (gender) as I can do if (both) then male || female type of line but the search part will have an issue if blank.

I've searched this board and the web but I apparently suck at searching because I couldn't find anyone having a combined search+dropdown filter combo. It seems one or the other but never both.

Any help you can give would be amazing thank you!

about 3 years ago · Juan Pablo Isaza
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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda