Как перебрать `address_component` для получения` страны` или любого - PullRequest
0 голосов
/ 27 апреля 2018

В моем приложении я получаю длинные данные от Google. я знаю, что мы можем повторить и получить значение. Но я уверен, что результат Google всегда будет таким же, как я получил. В таком случае, каков был бы правильный подход, чтобы название страны всегда было безопасным?

вот результат, который я получаю по умолчанию:

{
  "results": [
    {
      "address_components": [
        {
          "long_name": "49",
          "short_name": "49",
          "types": [
            "street_number"
          ]
        },
        {
          "long_name": "Sydenhams Road",
          "short_name": "Sydenhams Rd",
          "types": [
            "route"
          ]
        },
        {
          "long_name": "Park Town",
          "short_name": "Park Town",
          "types": [
            "political",
            "sublocality",
            "sublocality_level_1"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "locality",
            "political"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name": "Tamil Nadu",
          "short_name": "TN",
          "types": [
            "administrative_area_level_1",
            "political"
          ]
        },
        {
          "long_name": "India",
          "short_name": "IN",
          "types": [
            "country",
            "political"
          ]
        },
        {
          "long_name": "600003",
          "short_name": "600003",
          "types": [
            "postal_code"
          ]
        }
      ],
      "formatted_address": "49, Sydenhams Rd, Park Town, Chennai, Tamil Nadu 600003, India",
      "geometry": {
        "location": {
          "lat": 13.0826869,
          "lng": 80.27050659999999
        },
        "location_type": "ROOFTOP",
        "viewport": {
          "northeast": {
            "lat": 13.0840358802915,
            "lng": 80.2718555802915
          },
          "southwest": {
            "lat": 13.0813379197085,
            "lng": 80.2691576197085
          }
        }
      },
      "place_id": "ChIJ11Py-v1lUjoROzu_lR9o7io",
      "types": [
        "street_address"
      ]
    },
    {
      "address_components": [
        {
          "long_name": "Raja Flats",
          "short_name": "Raja Flats",
          "types": [
            "establishment",
            "point_of_interest"
          ]
        },
        {
          "long_name": "School Street",
          "short_name": "School St",
          "types": [
            "route"
          ]
        },
        {
          "long_name": "Srinivasa Nagar",
          "short_name": "Srinivasa Nagar",
          "types": [
            "political",
            "sublocality",
            "sublocality_level_2"
          ]
        },
        {
          "long_name": "Perambur",
          "short_name": "Perambur",
          "types": [
            "political",
            "sublocality",
            "sublocality_level_1"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "locality",
            "political"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name": "Tamil Nadu",
          "short_name": "TN",
          "types": [
            "administrative_area_level_1",
            "political"
          ]
        },
        {
          "long_name": "India",
          "short_name": "IN",
          "types": [
            "country",
            "political"
          ]
        },
        {
          "long_name": "600110",
          "short_name": "600110",
          "types": [
            "postal_code"
          ]
        }
      ],
      "formatted_address": "Raja Flats, 600003, School St, Srinivasa Nagar, Perambur, Chennai, Tamil Nadu 600110, India",
      "geometry": {
        "location": {
          "lat": 13.0826802,
          "lng": 80.2707184
        },
        "location_type": "GEOMETRIC_CENTER",
        "viewport": {
          "northeast": {
            "lat": 13.0840291802915,
            "lng": 80.27206738029152
          },
          "southwest": {
            "lat": 13.0813312197085,
            "lng": 80.26936941970851
          }
        }
      },
      "place_id": "ChIJkRKg-_1lUjoR5ts0tmVrxMg",
      "types": [
        "establishment",
        "point_of_interest"
      ]
    }
  ],
  "status": "OK"
}

Ответы [ 2 ]

0 голосов
/ 27 апреля 2018

В таком случае, каков будет правильный подход к получению страны? имя всегда безопасным способом?

Чтобы получить названия стран, используйте map, find и includes

var countries = obj.results.map( s => s.address_components.find ( t => t.types.includes( "country" ) ).long_name )

Чтобы получить уникальные страны, используйте оператор Set и распространение от ... до Array

countries = [...new Set( countries )];

Демо

var obj = {
  "results": [{
      "address_components": [{
          "long_name": "49",
          "short_name": "49",
          "types": [
            "street_number"
          ]
        },
        {
          "long_name": "Sydenhams Road",
          "short_name": "Sydenhams Rd",
          "types": [
            "route"
          ]
        },
        {
          "long_name": "Park Town",
          "short_name": "Park Town",
          "types": [
            "political",
            "sublocality",
            "sublocality_level_1"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "locality",
            "political"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name": "Tamil Nadu",
          "short_name": "TN",
          "types": [
            "administrative_area_level_1",
            "political"
          ]
        },
        {
          "long_name": "India",
          "short_name": "IN",
          "types": [
            "country",
            "political"
          ]
        },
        {
          "long_name": "600003",
          "short_name": "600003",
          "types": [
            "postal_code"
          ]
        }
      ],
      "formatted_address": "49, Sydenhams Rd, Park Town, Chennai, Tamil Nadu 600003, India",
      "geometry": {
        "location": {
          "lat": 13.0826869,
          "lng": 80.27050659999999
        },
        "location_type": "ROOFTOP",
        "viewport": {
          "northeast": {
            "lat": 13.0840358802915,
            "lng": 80.2718555802915
          },
          "southwest": {
            "lat": 13.0813379197085,
            "lng": 80.2691576197085
          }
        }
      },
      "place_id": "ChIJ11Py-v1lUjoROzu_lR9o7io",
      "types": [
        "street_address"
      ]
    },
    {
      "address_components": [{
          "long_name": "Raja Flats",
          "short_name": "Raja Flats",
          "types": [
            "establishment",
            "point_of_interest"
          ]
        },
        {
          "long_name": "School Street",
          "short_name": "School St",
          "types": [
            "route"
          ]
        },
        {
          "long_name": "Srinivasa Nagar",
          "short_name": "Srinivasa Nagar",
          "types": [
            "political",
            "sublocality",
            "sublocality_level_2"
          ]
        },
        {
          "long_name": "Perambur",
          "short_name": "Perambur",
          "types": [
            "political",
            "sublocality",
            "sublocality_level_1"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "locality",
            "political"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name": "Tamil Nadu",
          "short_name": "TN",
          "types": [
            "administrative_area_level_1",
            "political"
          ]
        },
        {
          "long_name": "India",
          "short_name": "IN",
          "types": [
            "country",
            "political"
          ]
        },
        {
          "long_name": "600110",
          "short_name": "600110",
          "types": [
            "postal_code"
          ]
        }
      ],
      "formatted_address": "Raja Flats, 600003, School St, Srinivasa Nagar, Perambur, Chennai, Tamil Nadu 600110, India",
      "geometry": {
        "location": {
          "lat": 13.0826802,
          "lng": 80.2707184
        },
        "location_type": "GEOMETRIC_CENTER",
        "viewport": {
          "northeast": {
            "lat": 13.0840291802915,
            "lng": 80.27206738029152
          },
          "southwest": {
            "lat": 13.0813312197085,
            "lng": 80.26936941970851
          }
        }
      },
      "place_id": "ChIJkRKg-_1lUjoR5ts0tmVrxMg",
      "types": [
        "establishment",
        "point_of_interest"
      ]
    }
  ],
  "status": "OK"
};

var countries = obj.results.map(s => s.address_components.find(t => t.types.includes("country")).long_name)

countries = [...new Set(countries)]; //comment this line if uniqueness is not required

console.log(countries);

Чтобы получить страну short_name, а также

Демо

var obj = {
  "results": [{
      "address_components": [{
          "long_name": "49",
          "short_name": "49",
          "types": [
            "street_number"
          ]
        },
        {
          "long_name": "Sydenhams Road",
          "short_name": "Sydenhams Rd",
          "types": [
            "route"
          ]
        },
        {
          "long_name": "Park Town",
          "short_name": "Park Town",
          "types": [
            "political",
            "sublocality",
            "sublocality_level_1"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "locality",
            "political"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name": "Tamil Nadu",
          "short_name": "TN",
          "types": [
            "administrative_area_level_1",
            "political"
          ]
        },
        {
          "long_name": "India",
          "short_name": "IN",
          "types": [
            "country",
            "political"
          ]
        },
        {
          "long_name": "600003",
          "short_name": "600003",
          "types": [
            "postal_code"
          ]
        }
      ],
      "formatted_address": "49, Sydenhams Rd, Park Town, Chennai, Tamil Nadu 600003, India",
      "geometry": {
        "location": {
          "lat": 13.0826869,
          "lng": 80.27050659999999
        },
        "location_type": "ROOFTOP",
        "viewport": {
          "northeast": {
            "lat": 13.0840358802915,
            "lng": 80.2718555802915
          },
          "southwest": {
            "lat": 13.0813379197085,
            "lng": 80.2691576197085
          }
        }
      },
      "place_id": "ChIJ11Py-v1lUjoROzu_lR9o7io",
      "types": [
        "street_address"
      ]
    },
    {
      "address_components": [{
          "long_name": "Raja Flats",
          "short_name": "Raja Flats",
          "types": [
            "establishment",
            "point_of_interest"
          ]
        },
        {
          "long_name": "School Street",
          "short_name": "School St",
          "types": [
            "route"
          ]
        },
        {
          "long_name": "Srinivasa Nagar",
          "short_name": "Srinivasa Nagar",
          "types": [
            "political",
            "sublocality",
            "sublocality_level_2"
          ]
        },
        {
          "long_name": "Perambur",
          "short_name": "Perambur",
          "types": [
            "political",
            "sublocality",
            "sublocality_level_1"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "locality",
            "political"
          ]
        },
        {
          "long_name": "Chennai",
          "short_name": "Chennai",
          "types": [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name": "Tamil Nadu",
          "short_name": "TN",
          "types": [
            "administrative_area_level_1",
            "political"
          ]
        },
        {
          "long_name": "India",
          "short_name": "IN",
          "types": [
            "country",
            "political"
          ]
        },
        {
          "long_name": "600110",
          "short_name": "600110",
          "types": [
            "postal_code"
          ]
        }
      ],
      "formatted_address": "Raja Flats, 600003, School St, Srinivasa Nagar, Perambur, Chennai, Tamil Nadu 600110, India",
      "geometry": {
        "location": {
          "lat": 13.0826802,
          "lng": 80.2707184
        },
        "location_type": "GEOMETRIC_CENTER",
        "viewport": {
          "northeast": {
            "lat": 13.0840291802915,
            "lng": 80.27206738029152
          },
          "southwest": {
            "lat": 13.0813312197085,
            "lng": 80.26936941970851
          }
        }
      },
      "place_id": "ChIJkRKg-_1lUjoR5ts0tmVrxMg",
      "types": [
        "establishment",
        "point_of_interest"
      ]
    }
  ],
  "status": "OK"
};

var countries = obj.results.map(s => {
   var obj = s.address_components.find(t => t.types.includes("country"));
   return { long_name : obj.long_name, short_name : obj.short_name };
})

//countries = [...new Set(countries)]; //comment this line if uniqueness is not required

console.log(countries);
0 голосов
/ 27 апреля 2018
var country = address_components.filter(component => component.types.includes("country"/* or any */))

Если !!country, то найдена страна и вы можете получить доступ к ее имени с помощью country.short_name или country.long_name. В противном случае нет страны в address_components

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...