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

98
Visualizações
How to apply filters together?

I have markers on a Google Map with dropdown filters, and these filters work perfectly well individually. However, I want the filter to consider all three drop-down lists at once.

Here is my code: https://jsfiddle.net/h4cv57tb/

Somehow the 3 filter functions should be merged, but unfortunately I have no idea how to ...

Here are the filter functions which should be merged:

//Filter by type
filterMarkers = function(category) {
                for (i = 0; i < gmarkers1.length; i++) {
                    marker = gmarkers1[i];
                    // If is same category or category not picked
                    if (marker.category == category || category.length === 0) {
                        marker.setVisible(true);
                    }
                    // Categories don't match
                    else {
                        marker.setVisible(false);
                    }
                }
            }
    
            //FILTER by ripening time
    
            filterMarkersEres = function(eres) {
                for (i = 0; i < gmarkers1.length; i++) {
                    marker = gmarkers1[i];
                    var ereshonap = marker.eres;
    
                    // If is same category or category not picked
                    if (ereshonap.includes(parseInt(eres)) || eres.length === 0) { //
                        marker.setVisible(true);
                    }
                    // Categories don't match
                    else {
                        marker.setVisible(false);
                    }
                }
            }
    
    
            //FILTER by place
            filterMarkersHely_tipus = function(hely_tipus) {
                for (i = 0; i < gmarkers1.length; i++) {
                    marker = gmarkers1[i];
                    // If is same category or category not picked
                    if (marker.hely_tipus == hely_tipus || hely_tipus.length === 0) {
                        marker.setVisible(true);
                    }
                    // Categories don't match
                    else {
                        marker.setVisible(false);
                    }
                }
            }

Thanks in advance for your help!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Make sure you look at where there is duplicate code and simply combine areas that are different, if feasible. To combine these functions into a single one just combine the if conditions together.

filterMarkers = function(category = null, eres = null, hely_tipus = null) {
    for (i = 0; i < gmarkers1.length; i++) {
        marker = gmarkers1[i];
        ereshonap = marker.eres;
        visible = false;

        if (category){
            if (marker.category == category || category.length === 0) {
            visible = true;
            }
        }

        if (eres) {
            if (ereshonap.includes(parseInt(eres)) || eres.length === 0) {
            visible = true;
            }
        }

        if (hely_tipus) {
            if(marker.hely_tipus == hely_tipus) {
                visible = true;
            }
        }

        if (visible) {
            marker.setVisible(true);
        } else {
            marker.setVisible(false);
        }
    }
}
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