如何迭代`address_component`来获取`country`或any

时间:2018-04-27 10:40:44

标签: javascript jquery typescript

在我的应用程序中,我从谷歌获取长数据。我知道我们可以迭代并获得价值。但我相信谷歌的结果总是和我一样。在那种情况下,以安全的方式获取国名的正确方法是什么?

这是我预设的结果:

{
  "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 :(得分:3)

  

在这种情况下,获得国家的正确方法是什么   名字总是以安全的方式?

要获取国家/地区名称,请使用mapfindincludes

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

要获取唯一的国家/地区,请使用设置和展开运算符...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);

答案 1 :(得分:0)

var country = address_components.filter(component => component.types.includes("country"/* or any */))

如果!!country,则找到了国家/地区,您可以使用country.short_namecountry.long_name访问其名称。否则,address_components

中没有国家/地区