how to filter a json object?

1

I have the following json object:

var json = {
    "acao": "listaHoteis",
    "hoteisPesquisa": [
        {
            "home_id": "1",
            "nome": "Itamarati"
            "preco": "925"
        }, {
            "home_id": "2",
            "nome": "copacabana"
            "preco": "102.1"
        }, {
            "home_id": "3",
            "nome": "Itamarati"
            "preco": "215"
        },{
            "home_id": "4",
            "nome": "Litoral Htel"
            "preco": "1001"
        }
    ]
};

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

var json = {
    "acao": "listaHoteis",
    "hoteisPesquisa": [
        {
            "home_id": "2",
            "nome": "copacabana"
            "preco": "102.1"
        }, {
            "home_id": "3",
            "nome": "Itamarati"
            "preco": "215"
        }
    ]
};

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

    
asked by anonymous 07.05.2017 / 13:35

2 answers

1

By your JSON I see that you have the prices in String . you need to convert to number and then use .filter() to remove the ones you do not want.

You can do it like this:

var json = {
  "acao": "listaHoteis",
  "hoteisPesquisa": [{
    "home_id": "1",
    "nome": "Itamarati",
    "preco": "925"
  }, {
    "home_id": "2",
    "nome": "copacabana",
    "preco": "102.1"
  }, {
    "home_id": "3",
    "nome": "Itamarati",
    "preco": "215"
  }, {
    "home_id": "4",
    "nome": "Litoral Hotel",
    "preco": "1001"
  }]
};

var filtrados = json.hoteisPesquisa.filter(function(hotel) {
  var preco = Number(hotel.preco);
  return preco > 100 && preco < 900;
});

console.log(filtrados);
    
07.05.2017 / 13:49
0

How do I filter the same way the first 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
                  },
               ]
            }
         ]
      }
   ]
};

In this price it is in:

hotelPesquisa[].quarto[].quartoUh[].tarifa.vlDiariaTotal
Can you filter the same way? greater than 100 and less than 900?     
08.05.2017 / 18:05