how to filter a json object?

Asked

Viewed 1,541 times

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 only show hotels where the price is higher than 100 and lower 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 maintain all properties? Thank you!

2 answers

2


By your JSON I see that you have the prices in String. you have to convert to number and then use .filter() to remove the ones you don’t 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);

  • 1

    Thank you worked perfectly.

0

How do I filter this json the same way the first time:

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

That’s where the price is:

hotelPesquisa[].quarto[].quartoUh[].tarifa.vlDiariaTotal
Can you filter the same way? Larger than 100 and smaller than 900?

Browser other questions tagged

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