gpt4 book ai didi

javascript - 如何迭代 `address_component` 以获得 `country` 或任何

转载 作者:行者123 更新时间:2023-11-28 17:32:12 24 4
gpt4 key购买 nike

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

这是我在预设处得到的结果:

{
"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"
}

最佳答案

In that case what would be the correct approach to get the country name always with safe way?

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

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);

关于javascript - 如何迭代 `address_component` 以获得 `country` 或任何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50060659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com