javascript - Given an array of JSON objects, how can I get JSON object X based on a nested value? -


i using google maps geocoding api following json object, based on 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 long_name , short_name of object in address_components array based on particular type?

example

i want long_name and short_name of objects have "locality" in types array.

context

i'm using information build "apartment finder" application using node.js, express, , mongodb.

considerations

i've read underscore library this.

edit: thoughts

so, have idea how search through address_components array. (i can access results[0]), , believe can access types array dot notation, want search through types arrays of each object , return when find object with, let's say, types containing 'location'. how can this?

i'm not experienced parsing/looping json. learning. appreciated. thank you!

you can use nested .filter objects containing that, combined .map create array of matching objects:

var objectscontainingconditions = data.results[0].address_components.filter(function(item) {     return item.types.filter(function(addresstype) {         return addresstype == "locality";     }).length > 0; }).map(function(item) {     return {         long: item.long_name,         short: item.short_name     } }); 

put function , pass type if want reuse it:

function querygooglecomponentsbytype(type, components) {     return components.filter(function(item) {         return item.types.filter(function(addresstype) {             return addresstype == type;         }).length > 0;     }).map(function(item) {         return {             long: item.long_name,             short: item.short_name         }     }); }  var results = querygooglecomponentsbytype("locality", data.results[0].address_components); 

demo: http://jsfiddle.net/upzjth30/


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -