0

Using Google's Maps API, I want to be able to find the value of a key within an object by the value within an array within the same object. When I "Inspect Element" on my page in order to view the console, here is what it shows in terms of object structure:

results: {
    address_components [
        0: Object: {
            long_name: "704",
            short_name: "704",
            types [
                0:"street_number"
            ]
        }
        1: Object {...}
        2: Object {...}
        3: Object {...}
    ]
    place_id:"8AS8D8F8A881C81DA6S6D8"
}

I want to be able to find "street_number" in the object so I can find the corresponding value of "704" that also resides in the object.

The tricky part is that the values within "address_compenents" are not always in the same order so I cannot just write results[0].address_components[0].long_name in my javascript to find it. I am restricted to javascript in this project so any answers in that language would be much appreciated. Thank you in advance!

Note: I am not opposed to using libraries like lodash or underscore if it helps solve the problem.

Austin
  • 13
  • 1
  • related question: [Reverse Geocoding example doesn't display the street name of a location in some regions](http://stackoverflow.com/questions/33656636/reverse-geocoding-example-doesnt-display-the-street-name-of-a-location-in-some) – geocodezip Jul 11 '16 at 18:58
  • related question: [Google Maps: how to get country, state/province/region, city given a lat/long value?](http://stackoverflow.com/questions/4013606/google-maps-how-to-get-country-state-province-region-city-given-a-lat-long-va) – geocodezip Jul 11 '16 at 19:02

2 Answers2

1

First find() the item, then read the required attribute.

Note that you should also think about and handle the case where there is no street_number in the response, which is not covered by this snippet.

var results = {
  address_components: [{
    long_name: "704",
    short_name: "704",
    types: [
      "street_number"
    ]
  }, {
    long_name: "100",
    short_name: "100",
    types: [
      "attribute_a"
    ]
  }, {
    long_name: "200",
    short_name: "200",
    types: [
      "attribute_b"
    ]
  }, {
    long_name: "300",
    short_name: "300",
    types: [
      "attribute_c"
    ]
  }],
  place_id: "8AS8D8F8A881C81DA6S6D8"
}

var streetNumber = results.address_components.find(function(item) {
  return item.types.some(function(subitem) {
    return subitem === 'street_number'
  });
}).long_name;

console.log(streetNumber); // 704
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
  • Thanks for a timely response! I happened to stumble upon an answer elsewhere after asking however, your answer would have also solved my problem. Thank you! – Austin Jul 12 '16 at 20:09
0

it is possible to do it by using Array.filter()

results: {
    address_components [
        0: Object: {
            long_name: "704",
            short_name: "704",
            types [
                0:"street_number"
            ]
        }
        1: Object {...}
        2: Object {...}
        3: Object {...}
    ]
    place_id:"8AS8D8F8A881C81DA6S6D8"
}


const result = results.address_components.find(item => item.types.indexOf('street_number') > -1)

const longName = result.long_name // 704
const shortName = result.short_name // 704
Alongkorn
  • 3,968
  • 1
  • 24
  • 41