Help with JavaScript filter

1

I have the following JavaScript code:

JSON

var json = {
       "tpAmbiente":null,
       "hotelPesquisa":[
          {
             "dtEntrada":"20170510",
             "dtSaida":"20170511",
             "hotel":{
                "id":94,
                "nome":"Itamarati"
             },
             "quarto":[
                {
                   "quartoUh":[
                      {
                        "nQUarto": 1,
                         "tarifa":{
                            "vlDiariaTotal":157.21,
                            "desconto":null
                         },
                         "qtDisponivel":null,
                         "desconto":null
                      },
                      {
                        "nQUarto": 2,
                         "tarifa":{
                            "vlDiariaTotal":157.21,
                            "desconto":null
                         },
                         "qtDisponivel":null,
                         "desconto":null
                      },
                   ]
                }
             ]
          },
          {
             "dtEntrada":"20170510",
             "dtSaida":"20170511",
             "hotel":{
                "id":95,
                "nome":"copacabana"
             },
             "quarto":[
                {
                   "quartoUh":[
                      {
                        "nQUarto": 1,
                         "tarifa":{
                            "vlDiariaTotal":102.1,
                            "desconto":null
                         },
                         "qtDisponivel":null,
                         "desconto":null
                      },
                      {
                        "nQUarto": 2,
                         "tarifa":{
                            "vlDiariaTotal":102.1,
                            "desconto":null
                         },
                         "qtDisponivel":null,
                         "desconto":null
                      },
                   ]
                }
             ]
          },
          {
             "dtEntrada":"20170510",
             "dtSaida":"20170511",
             "hotel":{
                "id":96,
                "nome":"Itamarati"
             },
             "quarto":[
                {
                   "quartoUh":[
                      {
                        "nQUarto": 1,
                         "tarifa":{
                            "vlDiariaTotal":157.21,
                            "desconto":null
                         },
                         "qtDisponivel":null,
                         "desconto":null
                      },
                      {
                        "nQUarto": 2,
                         "tarifa":{
                            "vlDiariaTotal":157.21,
                            "desconto":null
                         },
                         "qtDisponivel":null,
                         "desconto":null
                      },
                   ]
                }
             ]
          },
          {
             "dtEntrada":"20170510",
             "dtSaida":"20170511",
             "hotel":{
                "id":96,
                "nome":"Litoral Hotel"
             },
             "quarto":[
                {
                   "quartoUh":[
                      {
                        "nQUarto": 1,
                         "tarifa":{
                            "vlDiariaTotal":1001.00,
                            "desconto":null
                         },
                         "qtDisponivel":null,
                         "desconto":null
                      },
                      {
                        "nQUarto": 2,
                         "tarifa":{
                            "vlDiariaTotal":1001.00,
                            "desconto":null
                         },
                         "qtDisponivel":null,
                         "desconto":null
                      },
                   ]
                }
             ]
          }
       ]
    };

Function

function filtro(min, max){
    var pesquisa = {
      min: min,
      max: max
    };
    var filtrados = json.hotelPesquisa.filter(function(hotel) {
        hotel.quarto[0].quartoUh = hotel.quarto[0].quartoUh.filter(function(quarto) {
            return quarto.tarifa.vlDiariaTotal  dados.min;
        });
        return hotel.quarto[0].quartoUh.length > 0;
    });
    console.log(filtrados);
}

I use the Jquery UI slider that range the code and:

$("#price-range").slider({
    range: true,
    min: 0,
    max: 1000,
    values: [ 0, 1000 ],
    slide: function( event, ui ) {
        tjq(".min-price-label").html(ui.values[ 0 ]);
        tjq(".max-price-label").html(ui.values[ 1 ]);
        filtro(ui.values[ 0 ], ui.values[ 1 ]);
    }
});

When I drag the slider it calls the filter function by passing the parameters min and max to be able to filter the json with the minimum and maximum values of the slider. in the beginning it lays filters but after dragging the slider several times it returns empty:

[]

DoesanyoneknowhowIcanresolvethis?


Example - JSFiddle Home The response appears in the console.

    
asked by anonymous 10.05.2017 / 20:44

1 answer

1

The problem is that the value of the array was being superimposed on hotel.quarto[0].quartoUh = hotel.quarto[0].quartoUh.filter(... .

Using

var filtrados = json.hotelPesquisa.filter(function(hotel) {
    var quartos = hotel.quarto[0].quartoUh.filter(function(quarto) {
        return quarto.tarifa.vlDiariaTotal < pesquisa.max && quarto.tarifa.vlDiariaTotal > pesquisa.min;
    });
    return quartos.length > 0;
});

works fine ( link ). But to keep the part filtering rooms there are also different alternatives, one of them might be to create clones like this: link

    
13.05.2017 / 13:48