How to filter a json object with jQuery?

Asked

Viewed 276 times

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 only show hotels where the price is higher than 100 and lower than 900?

The price stays on:

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 maintain all properties? Thank you!

  • You want to remove the rooms that are out of price or keep the rooms but remove the hotel where none of the rooms is within price?

  • Is it possible to do both? But I think it is better to remove the hotel

  • It’s possible to take hotels and rooms, that’s what you’re looking for?

  • That’s right. In some cases I need to take away the hotels and there are other cases I need to take away the rooms

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/ your answer.

In this case you need to repeat the logic since you want to filter into 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);

  • 1

    It worked. Thank you!! Sorry anything. It helped a lot.

  • 1

    @Newtech great!

  • you know what it could be? https://answall.com/questions/203698/help-com-filtro-javascript

  • @Newtech erased the question. You found the solution?

  • Not yet. My problem and the next. I need the filter to return me a json with all hotels where room prices are higher than 0 and lower than 1010 with manually set values in an array it filters normal but when using the slide range it does not filter. it starts to filter when it slides the slide range but then starts to return only [] as shown IN THAT IMAGE jsfiddle link looks at https://jsfiddle.net/newtech/xrkuoqhq/2console/

  • @Newtech here in Sweden where I live is late so I’m going to sleep. But tomorrow I’m here. Ask a question I’ll answer if no one’s answered it yet :)

  • in the message I send above has the link of an image and code in jsfiddle and I retrieved the question: https://answall.com/questions/203698/help-com-filtro-javascript

Show 2 more comments

Browser other questions tagged

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