我有这个对象
{
"results": [
{
"address_components": [
{
"long_name": "Abdon Cifuentes - Carlos Silva Vildosola",
"short_name": "Abdon Cifuentes - Carlos Silva Vildosola",
"types": [
"point_of_interest",
"establishment"
]
},
{
"long_name": "Concepción",
"short_name": "Concepción",
"types": [
"locality",
"political"
]
},
{
"long_name": "Concepción",
"short_name": "Concepción",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Concepcion",
"short_name": "Concepcion",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Región del Bío Bío",
"short_name": "Región del Bío Bío",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Chile",
"short_name": "CL",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Abdon Cifuentes - Carlos Silva Vildosola, Concepción, Concepción, Región del Bío Bío, Chile",
"geometry": {
"location": {
"lat": -36.8000307,
"lng": -73.0487725
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": -36.7986817197085,
"lng": -73.0474235197085
},
"southwest": {
"lat": -36.8013796802915,
"lng": -73.05012148029151
}
}
},
"place_id": "ChIJxy0y5a61aZYRq9rC4fyIYfg",
"types": [
"bus_station",
"transit_station",
"point_of_interest",
"establishment"
]
},
{
"address_components": [
{
"long_name": "Concepción",
"short_name": "Concepción",
"types": [
"locality",
"political"
]
},
{
"long_name": "Concepción",
"short_name": "Concepción",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Concepcion",
"short_name": "Concepcion",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Región del Bío Bío",
"short_name": "Región del Bío Bío",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Chile",
"short_name": "CL",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Concepción, Concepción, Región del Bío Bío, Chile",
"geometry": {
"bounds": {
"northeast": {
"lat": -36.76354250000001,
"lng": -72.98195799999999
},
"southwest": {
"lat": -36.8890559,
"lng": -73.0841332
}
},
"location": {
"lat": -36.8201352,
"lng": -73.0443904
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": -36.76354250000001,
"lng": -72.98195799999999
},
"southwest": {
"lat": -36.8890559,
"lng": -73.0841332
}
}
},
"place_id": "ChIJO8a70s21aZYRmCYhUrc3y0Q",
"types": [
"locality",
"political"
]
}
],
"status": "OK"
}
我需要的是在每个结果[i] .types数组中搜索的函数,如果函数找到结果[i] .types [j] ==“locality”返回结果[i]。因此,如果结果[i] .types [j] ==“locality”没有发现任何巧合,则不返回任何内容。
答案 0 :(得分:1)
<强> jsBin demo 强>
console.log( ob.results[1].types )
将记录此结果:["locality", "political"]
并且formatted_adress
内的notihng(就像你想的那样)导致该属性只包含一个字符串"Concepción, Concepción, Reg..."
获取整个"address_components"
(虽然未在您的问题中说明)使用:
ob.results[1].address_components
如果您想要整个第二个对象(来自您的"results"
数组),请使用:
ob.results[1]; // 0 would get the first one, so use 1
如果要匹配"locality"
数组中字符串types
的存在,请执行
var result; // Will hold the entire array if we found "locality" inside the `types` Array
for(var i=0; i<ob.results.length; i++){
var loc = ob.results[i]; // Iterate all locations
if(loc.hasOwnProperty("types") && loc.types.indexOf("locality") != -1){
result = loc;
break; // Finish iterating once we found the first occurrence
}
}
console.log( result ); // The 2nd ([1]) array
<强> jsBin demo 强>
答案 1 :(得分:1)
使用Array.prototype.filter()缩小locality
类型
var filtered_results = data.results.filter(function(item){
return item.types.indexOf('locality') >-1;
});
然后从过滤后的数组中取出第一个
var addressParts
if(filtered_results.length){
addressParts = filtered_components[0].address_components;
}