jeudi 4 février 2016

Find value of key based on value in array within that key

This is a brain teaser, so I'm using the places API (using an addon for ember.js so I don't really have control over the version unless I wanted to write my own) and it returns me data that looks like this

{
  "address_components": [
    {
      "long_name": "1235",
      "short_name": "1235",
      "types": [
        "street_number"
      ]
    },
    {
      "long_name": "Long Point Road",
      "short_name": "Long Point Rd",
      "types": [
        "route"
      ]
    },
    {
      "long_name": "Mount Pleasant",
      "short_name": "Mt Pleasant",
      "types": [
        "locality",
        "political"
      ]
    },
    {
      "long_name": "Charleston County",
      "short_name": "Charleston County",
      "types": [
        "administrative_area_level_2",
        "political"
      ]
    },
    {
      "long_name": "South Carolina",
      "short_name": "SC",
      "types": [
        "administrative_area_level_1",
        "political"
      ]
    },
    {
      "long_name": "United States",
      "short_name": "US",
      "types": [
        "country",
        "political"
      ]
    },
    {
      "long_name": "29464",
      "short_name": "29464",
      "types": [
        "postal_code"
      ]
    },
    {
      "long_name": "9020",
      "short_name": "9020",
      "types": [
        "postal_code_suffix"
      ]
    }
  ],
  "adr_address": "<span class=\"street-address\">1235 Long Point Rd</span>, <span class=\"locality\">Mt Pleasant</span>, <span class=\"region\">SC</span> <span class=\"postal-code\">29464-9020</span>, <span class=\"country-name\">USA</span>",
  "formatted_address": "1235 Long Point Rd, Mt Pleasant, SC 29464, USA",
  "geometry": {
    "location": {
      "lat": 32.8570647,
      "lng": -79.8224902
    }
  },
  "icon": "http://ift.tt/1OnrNoX",
  "id": "f3d33a1a65adc47f7fcf927bb7b18ea91a59fb81",
  "name": "1235 Long Point Rd",
  "place_id": "ChIJkVjE8_5t_ogRvHeqOGZ6cB0",
  "reference": "CpQBjAAAAJEVbHvc6lTEXUvlKHc-wTI3f8g8U6W7i6oARvPyP_2TWTNE7IJQQHTABvbxTtMK6cFxRaksi2xaT7lcQiRB-nv7co7BVzfUMrwqr0YFQZvbLH_OGflPlO---vX4GPTQlDnFinnnixHW7LcRGbPJ4jfgnQ3HnURUCUmCa0WrLTIxiuqEwzIljAGv9K_iUMU9nBIQE57UebJ0CfCs9y0MBBvGeRoUUovGUbFYmgCnPOKVlXhrLQaE5uY",
  "scope": "GOOGLE",
  "types": [
    "street_address"
  ],
  "url": "http://ift.tt/1PlFSzC",
  "vicinity": "Mount Pleasant",
  "html_attributions": []
}

So in the address_components part of the object you can see each object has a types key with values that pertain to what type they are, such as street_number or route. What I want to do is get the short_name (or long_name) of the object where type contains street_number or route and so on and so forth and be able to set those to variables or use them in some other way.

I've tried a few addresses and it returns them in roughly the same order, however sometimes there are one more or one less item in that array so it's not always returning the same amount of items in the exact same order. So rather than try to look at index and hope that's the right value I'm expecting I want to get these values based on what type says.

I am using ember.js so if there is a computed property way of possibly doing this that would be cool, but I'm open to using javascript/jQuery functions to do this.

Aucun commentaire:

Enregistrer un commentaire