Home > Enterprise >  Jquery inArray return true for non-existent object
Jquery inArray return true for non-existent object

Time:06-11

Nominatim reverse send that:

{
"place_id":118873576,
"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type":"way",
"osm_id":71122987,
"lat":"48.86742742576341",
"lon":"2.334017224974853",
"display_name":"Rue des Petits Champs, Quartier du Palais Royal, Paris 1er Arrondissement, Paris, Île-de-France, France métropolitaine, 75001, France",
"address":{
"road":"Rue des Petits Champs",
"city_block":"Quartier du Palais Royal",
"neighbourhood":"Paris 1er Arrondissement",
"suburb":"Paris",
"city_district":"Paris",
"county":"Paris",
"state":"Île-de-France",
"region":"France métropolitaine",
"postcode":"75001",
"country":"France",
"country_code":"fr"},
"boundingbox":["48.8671374","48.8675112","2.3336619","2.3352462"]
}

If I want Neighbourhood only I do that :

if($.inArray("neighbourhood", address) !== -1){
                    console.log(' neighbourhood only ');
                    result = address.neighbourhood;
                }

Generally I need only city but neighbourhood display too (This last is used for capital and big city).

if($.inArray("city", response) !== 0){
                    console.log(' city only ';
                    result = address.city;
                }

My function ($.inArray) or the condition (!== -1) seems wrong because even the object doesn't exist the condition return true and consider the condition

With this array object and these two condition the console display: neighbourhood only and city only (but 'city' doesn't exist)

this problem can be repeated with other key : suburb and town. if two or more key object exist inside array object there are a conflict and nothing display (using the same condition)

CodePudding user response:

$.inArray is for arrays, use in for objects

if("city" in adress){
    console.log(' city only ';
    result = address.city;
}
  • Related