Problem with jQuery filter?

Asked

Viewed 88 times

2

I have this filter in jQuery:

    var aeroportosida = [];
    Filtro.prototype.filtroAereoNomeIda = function(value){
        var dataa = JSON.parse(JSON.stringify(this.data));
        aeroportosida = [];
        if(value.length == 1){
            var nm = value[0].split('- (')[1].split('|')[0];
            var nomev = nm.substr(0,(nm.length - 1));
            //var nomev = value[0].split('- ')[1].substr(1, (value[0].split('|')[0].split('- ')[1].substr(1).length -1));
            var precov = value[0].split('|')[1];
            Array.prototype.push.apply(aeroportosida, this.data.filter(function(pesquisa){
                var resposta = pesquisa.trecho[0].voo.filter(function(voos){
                    var nome = voos.aeroportoOrigem.sgIata;
                    var preco = voos.vooTarifa.tarifa.vlPagamento;
                    return nome == nomev && preco == precov;
                });
                pesquisa.trecho[0].voo = resposta;
                return resposta.length > 0;
            }));
        }else if(value.length > 1){
            $.grep(value, function(n, i){
                var nm = n.split('- (')[1].split('|')[0];
                var nomev = nm.substr(0,(nm.length - 1));
                var precov = n.split('|')[1];
                Array.prototype.push.apply(aeroportosida, dataa.filter(function(pesquisa){
                    var respostas = pesquisa.trecho[0].voo.filter(function(voos){
                        var nome = voos.aeroportoOrigem.sgIata;
                        var preco = voos.vooTarifa.tarifa.vlPagamento;
                        return nome != nomev && preco != precov;
                    });
                    pesquisa.trecho[0].voo = respostas;
                    return respostas.length > 0;
                }));
            });
        }else if(value.length == 0){
            return this;
        }
        this.data = aeroportosida;
        return this;
    }

It filters according to a list of checkbox. When I select only 1 checkbox it filters normally, but when I select more than 1 it does not filter, and returns null.

In the value parameter I pass the values of the checkbox selected in array.

Does anyone know why he can’t filter more than 1?

I need to filter a JSON according to the array data. For example if I pass two values in the array I have to filter the json by the first value after refilling the json by the second value.

I made this filter on top of this JSFIDDLE that came out of that question

Update: The error happens when I use the

var respostas = pesquisa.trecho[0].voo.filter(function(voos){

WITHIN

Array.prototype.push.apply(aeroportosida, dataa.filter(function(pesquisa){

Example: https://jsfiddle.net/nmx4dcc8/

Note that in the: {"sqTrecho":1,"voo":[]} the voo: cannot return []

  • what comes in console.log(search)?

  • @Diegoschmidt when using Filter.filtroAereoNomeIda(["...", "...", "...]); with an array with more than one data it returns the query.snippet[0]. flight equal to [] what cannot happen. If it returns like this error occurs further ahead. I made this filter on top of this one: https://jsfiddle.net/xrkuoqhq/9/

  • Can assemble an example by working with your javascript function?

  • @Diegoschmidt Follow an example: https://jsfiddle.net/nmx4dcc8/1/ Note that in {"sqTrecho":1,"voo":[]} the voo cannot return []

No answers

Browser other questions tagged

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