How to filter a json object with jQuery?

1

I have the following json object:

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
                  },
               ]
            }
         ]
      }
   ]
};

How do I filter this object so that I can only show hotels where the price is higher than 100 and less than 900?

Price is in:

hotelPesquisa[].quarto[].quartoUh[].tarifa.vlDiariaTotal

Return I need:

"hotelPesquisa":[
      {
         "dtEntrada":"20170510",
         "dtSaida":"20170511",
         "hotel":{
            "id":94,
            "nome":"Itamarati"
         },
         "quarto":[
            {
               "quartoUh":[
                  {
                    "nQUarto": 1,
                     "tarifa":{
                        "vlDiariaTotal":890.21,
                        "desconto":null
                     },
                     "qtDisponivel":null,
                     "desconto":null
                  },
                  {
                    "nQUarto": 2,
                     "tarifa":{
                        "vlDiariaTotal":890.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":890.21,
                        "desconto":null
                     },
                     "qtDisponivel":null,
                     "desconto":null
                  },
                  {
                    "nQUarto": 2,
                     "tarifa":{
                        "vlDiariaTotal":890.21,
                        "desconto":null
                     },
                     "qtDisponivel":null,
                     "desconto":null
                  },
               ]
            }
         ]
      }
   ]

Is there any way I can filter the object with jQuery and keep all properties? Thanks!

    
asked by anonymous 08.05.2017 / 18:55

1 answer

2

Analyze where the array you want to filter is and what the comparison element is. From there the logic is the same as this other question / answer of yours.

In this case you need to repeat the logic once you want to filter in two levels:

var pesquisa = {
  min: 100,
  max: 900
};
var hotelPesquisa = [{
    "dtEntrada": "20170510",
    "dtSaida": "20170511",
    "hotel": {
      "id": 94,
      "nome": "Itamarati"
    },
    "quarto": [{
      "quartoUh": [{
          "nQUarto": 1,
          "tarifa": {
            "vlDiariaTotal": 890.21,
            "desconto": null
          },
          "qtDisponivel": null,
          "desconto": null
        },
        {
          "nQUarto": 2,
          "tarifa": {
            "vlDiariaTotal": 890.21,
            "desconto": null
          },
          "qtDisponivel": null,
          "desconto": null
        },
      ]
    }]
  },
  {
    "dtEntrada": "20170510",
    "dtSaida": "20170511",
    "hotel": {
      "id": 95,
      "nome": "copacabana"
    },
    "quarto": [{
      "quartoUh": [{
          "nQUarto": 1,
          "tarifa": {
            "vlDiariaTotal": 0, // este é gratuito!
            "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": 890.21,
            "desconto": null
          },
          "qtDisponivel": null,
          "desconto": null
        },
        {
          "nQUarto": 2,
          "tarifa": {
            "vlDiariaTotal": 10000.21, // este é muito caro!
            "desconto": null
          },
          "qtDisponivel": null,
          "desconto": null
        },
      ]
    }]
  }
];

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

console.log(filtrados);
    
08.05.2017 / 20:33