- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我从谷歌获取长数据。我知道我们可以迭代并获得值。但我确信谷歌的结果总是和我得到的一样。在这种情况下,始终以安全的方式获取国家/地区名称的正确方法是什么?
这是我在预设处得到的结果:
{
"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?
要获取国家/地区名称,请使用 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);
关于javascript - 如何迭代 `address_component` 以获得 `country` 或任何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50060659/
我正在为我的雇主编写脚本,以从他们自己的站点获取某些数据。出于一长串原因,我需要从网站上获取数据,如图所示。我发现,其中一些数据是通过 js 调用检索的... 回想起来,我应该选择 Mechanize
我正在使用 python 和 cryptography.io 来签署和验证消息。我可以通过以下方式获得签名的 DER 编码字节表示: cryptography_priv_key.sign(messag
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
是否可以区分 ECDF?以下面得到的为例。 set.seed(1) a <- sort(rnorm(100)) b <- ecdf(a) plot(b) 我想对 b 求导以获得它的概率密度函数 (PD
我找到了如何从 navigator.mimeTypes 获取 mimetypes: function GetMimeTypes() { var message = ""; var mi
我在表单中使用单选按钮来隐藏/显示联系人表单中的成员 ID 字段。问题是,当 javascript 更改 html 中包含的隐藏 id 字段(该字段设置为“无”值)时,该字段将不再通过 post 可用
我正在做单元测试。我必须测试所有可能的if..else情况。但是在此if语句中: int32_t i32Res = snprintf(buffer, len, "The%d_String_%d", 0
我有一个 Facebook 应用程序,我想从中获取“喜欢”的总数。我想知道这是否可能。 其中 ID 是应用程序的 ID,ACCESS_TOKEN 是我尝试过的应用程序的当前访问 token : gra
如果我有多个计算实例尝试同时获取同一个 blob 的租约,则似乎经常会成功。我的印象是,一旦租约发出(并因此被客户获得),就不可能同时发出另一个租约? 我希望情况确实如此,我一直在 Azure 中使用
这是我的索引 POST /blogs/1 { "name" : "learn java", "popularity" : 100 } POST /blogs/2 { "name" : "l
我正在将 Symfony2 与 FOSUserBundle 一起使用。我需要为用户获得最高角色。 role_hierarchy: ROLE_CONTRIBUTOR: ROLE_USER
我正在向服务器发送基于 REST 的请求。我希望尽快得到答复,并希望了解可以进行的各种优化。 一种方法当然是在线程中并行发送这些请求。还有哪些其他选项可用于优化此功能? 在服务器上,可以添加哪些配置?
这可能是某种重复的问题,但我似乎找不到合适的解决方案。我正在使用 git4idea.history.GitHistoryUtils.history() 获取提交列表。如果 checkout 其中一个较
我正在做一个程序,可以输入每周的工资和那一周的总工作时间。它应该以小时工资率显示答案。但是我无法显示正确的“centavos/2 decimal places”公式并且它不想使用 float % fl
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我已经尝试了 mContext.getMainLooper() 和 Looper.getMainLooper()。两者都返回相同的结果,但我想知道哪种方法正确? 我还从 Android 开发人员链接中
我有一个“affiliates”表,其中包含“user”和“referredBy”列。 给定一个用户,我希望获得该用户推荐的所有“n 级”玩家。对于 n=1,我们只关心您直接推荐的玩家数量: SELE
我在 PostgreSQL 9.5 数据库中有两个表: project - id - name task - id - project_id - name - updated_
请帮助我怎样才能得到我预期的结果,在此先感谢并抱歉我的英语不好。 PHP: $dog = implode(',', $data['dogbreed']); $query .= "AND `do
我有 let impulse = CGVectorMake(CGFloat(Constants.impulse), 0) 如何在不创建另一个 CGVector 的情况下得到它的负值? 我正在考虑在 C
我是一名优秀的程序员,十分优秀!