I am using google maps geocoding api to get the following JSON object, based on an address:
{
"results":[
{
"address_components":[
{
"long_name":"643",
"short_name":"643",
"types":[
"subpremise"
]
},
{
"long_name":"900",
"short_name":"900",
"types":[
"street_number"
]
},
{
"long_name":"Folsom Street",
"short_name":"Folsom St",
"types":[
"route"
]
},
{
"long_name":"South of Market",
"short_name":"South of Market",
"types":[
"neighborhood",
"political"
]
},
{
"long_name":"San Francisco",
"short_name":"SF",
"types":[
"locality",
"political"
]
},
{
"long_name":"San Francisco County",
"short_name":"San Francisco County",
"types":[
"administrative_area_level_2",
"political"
]
},
{
"long_name":"California",
"short_name":"CA",
"types":[
"administrative_area_level_1",
"political"
]
},
{
"long_name":"United States",
"short_name":"US",
"types":[
"country",
"political"
]
},
{
"long_name":"94107",
"short_name":"94107",
"types":[
"postal_code"
]
},
{
"long_name":"1007",
"short_name":"1007",
"types":[
"postal_code_suffix"
]
}
]
}
]
}
Question
How can I get the long_name
and short_name
of an object in the address_components
array based on a particular type?
Example
I want to get the long_name
and short_name
of those objects that have "locality"
in their types
array.
Context
I'm using this information to build an "apartment finder" application using node.js, express, and mongodb.
Considerations
I've read that underscore library is good for this.
Edit: My Thoughts
So, I have an idea how to search through the address_components
array. (I can access it by results[0]
), and I believe I can access types
array by dot notation, but I want to search through all the types
arrays of each object and return when I find an object with, let's say, types
containing 'location'. How can I do this?
I'm not that well experienced parsing/looping JSON. I am learning. Any help is greatly appreciated. Thank you!