Javascript – Variable combination – Filter

Question:

I need a tip. I'm not a programmer, but I like to take risks automating processes, sorry if the question is too basic.

I'm working with the Google Maps API , and at some point I need to perform a filter to target the content of my InfoWindow .

I have a map with several filters, which apply to bookmarks and should extend to InfoWindow content.

Doing it this way, I'm taking the full content of JSON ards and printing to InfoWindow , without segmenting the filters.

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
    return n.ARMARIO_ERB===marker.getTitle();
});

for (var i=0;i<as.length;i++)
{ .... }

I have a series of filters, which segment my bookmarks, but I would like the InfoWindow content to show only records referring to filters.

//informação dos selects
var cluster = $( "#CLUSTER-select" ).val();
var cliente_recente = $('#cli_re-select').val();
var tipo_cli = $('#tipo_cli-select').val();
var tipo_reclama = $('#tipo_recla-select').val();

I would like a way to apply the filters at this point, testing if they are different from ZERO apply to . filter .

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
    return n.ARMARIO_ERB===marker.getTitle() && n.DESC_CLUSTER===cluster;
});

Add the && as filters are selected.

Is there any way more practice to do this without having to create a lot of IF nested testing the combinations?

I can select only one filter, or two, three and etc. are independent. Here is a sample of only 4, I have around 10. How can I do this more efficiently?

—————– Update —————-

I came back from lunch with an idea, but it didn't work out right.

I created the following structure:

var combina = [];
combina = [];

if(cluster !== '0') {
    combina.push('DESC_CLUSTER;' + cluster);
}
if(cliente_recente !== '0') {
    combina.push('CLIENTE_RECENTE;' + cliente_recente);
}
if(tipo_cli !== '0') {
    combina.push('TIPO_CLIENTE;' + tipo_cli);
}
if(tipo_reclama !== '0') {
    combina.push('TIPO_RECLAMACAO;' + tipo_reclama);
}

var filtro_add;
filtro_add = "1 = 1";

for(var i=0;i<combina.length;i++){
    var split;
    split = combina[i].split(';');
    filtro_add += " && n." + split[0] + " === " + '"' + split[1] + '"';
}

Result of filtro_add : 1 = 1 && n.DESC_CLUSTER === "CURITIBA" && n.TIPO_CLIENTE === "SOHO"

And I added this variable to my filter:

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
    return n.ARMARIO_ERB===marker.getTitle() && filtro_add;
});

But it didn't work, it doesn't apply the values ​​to the result. If I put the fields by hand it works. Example:

return n.ARMARIO_ERB===marker.getTitle() && n.DESC_CLUSTER === "CURITIBA";

How can I transform this String ?

—————– Update —————-

After a lot of research, I found an alternative, but with a bad performance and possible problems, the staff does not recommend its use very much. Through eval()

I added it to the search field.

The String looked like this: filtro_add : n.ARMARIO_ERB===marker.getTitle() && n.DESC_CLUSTER === "CURITIBA"

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
    return eval(filtro_add);
});

It works, but with terrible performance.

Is there any other alternative?

Thanks.

Answer:

I managed to perform the filter.

Thinking more calmly, it even seemed obvious. Living and learning.

Here's an example of how I did it:

//informação dos selects
var cluster = $( "#CLUSTER-select" ).val();
var cliente_recente = $('#cli_re-select').val();
var tipo_cli = $('#tipo_cli-select').val();
var tipo_reclama = $('#tipo_recla-select').val();                   

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
var filtro_add=true;

if(cluster !== '0') {
    filtro_add = filtro_add && n.DESC_CLUSTER === cluster;
}
if(cliente_recente !== '0') {
    filtro_add = filtro_add && n.CLIENTE_RECENTE === cliente_recente;
}
if(tipo_cli !== '0') {
    filtro_add = filtro_add && n.TIPO_CLIENTE === tipo_cli;
}
if(tipo_reclama !== '0') {
    filtro_add = filtro_add && n.TIPO_RECLAMACAO === tipo_reclama;
}

    return n.ARMARIO_ERB===marker.getTitle() && filtro_add;

});
Scroll to Top
AllEscort