Javascript - Combination of variables - Filter

Asked

Viewed 367 times

3

I’m in need of a tip. I’m not a programmer, but I like to risk automating processes, sorry if the question is too basic.

I’m working with the Google Maps API, and at a certain point I need to perform a filter to segment the contents of my InfoWindow.

I have a map with several filters, which apply to the markers and should extend to the contents of InfoWindow.

Doing it this way, I’m picking it up the full content of JSON ards and printing on 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 target my markers, but I would like in the content of InfoWindow only records referring to filters appear.

//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 applies in .filter.

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

Go adding the && as filters are selected.

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

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

----------------- Updating ----------------

I came back from lunch with an idea, but it didn’t work 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 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 in hand it works. Example:

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

How can I transform this String?

----------------- Updating ----------------

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

I added it to the search field.

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);
});

Works, but with poor performance.

Is there any other alternative?

Thank you.

1 answer

0


I was able to filter.

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

Here’s 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;

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.