数组按键值访问

时间:2015-07-25 21:22:07

标签: arrays

我目前正在使用以下方式访问数组:

address_components[0]['long_name'];
address_components[1]['long_name'];
address_components[4]['long_name'];
address_components[5]['long_name'];

然而,阵列有时会包含我不想包含在其中的信息,我无法控制哪些信息会弄乱我的数字。

   "address_components" : [
         {
            "long_name" : "2012",
            "short_name" : "2012",
            "types" : [ "street_number" ]
         },
         {
            "long_name" : "Wildwood Dr",
            "short_name" : "Wildwood Dr",
            "types" : [ "route" ]
         },
         {
            "long_name" : "St Augustine",
            "short_name" : "St Augustine",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "St. Johns County",
            "short_name" : "St Johns County",
            "types" : [ "administrative_area_level_2", "political" ]
         },
         {
            "long_name" : "Florida",
            "short_name" : "FL",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "United States",
            "short_name" : "US",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "32086",
            "short_name" : "32086",
            "types" : [ "postal_code" ]
         }
      ],

但有时结果如下:

"address_components" : [
         {
            "long_name" : "Amazon Fullfillment Center TPA1",
            "short_name" : "Amazon Fullfillment Center TPA1",
            "types" : [ "premise" ]
         },
         {
            "long_name" : "3350",
            "short_name" : "3350",
            "types" : [ "street_number" ]
         },
         {
            "long_name" : "Laurel Ridge Ave",
            "short_name" : "Laurel Ridge Ave",
            "types" : [ "route" ]
         },
         {
            "long_name" : "Ruskin",
            "short_name" : "Ruskin",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "Hillsborough County",
            "short_name" : "Hillsborough County",
            "types" : [ "administrative_area_level_2", "political" ]
         },
         {
            "long_name" : "Florida",
            "short_name" : "FL",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "United States",
            "short_name" : "US",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "33570",
            "short_name" : "33570",
            "types" : [ "postal_code" ]
         }
      ],

我如何访问说出street_number long_name,无论它在哪个位置?

3 个答案:

答案 0 :(得分:2)

看起来像JSON,所以我假设您正在使用JavaScript。

你这样做就像你告诉人类这样做。

"在这里,您可以看到某个组件是否有"street_number"作为其中一种类型。"

component.types.indexOf("street_number") >= 0;

"现在查找将"street_number"作为其中一种类型的组件。"

street_number_components = address_components.filter(function(c) {
    return c.types.indexOf("street_number") >= 0;
});

"获取您找到的第一个组件并提取其long_name。"

street_number_components[0].long_name;

把它们放在一起:

(address_components.filter(function(c) {
    return c.types.indexOf("street_number") >= 0;
}))[0].long_name;

答案 1 :(得分:0)

假设您谈论c#(或.NET),我建议使用Dictionary或List对象。

答案 2 :(得分:0)

如果您无法控制输入数据结构,则可以始终循环访问它并测试每个元素的类型。不知道你在用什么语言,但这就是想法。

for element in input:
    if element["types"][0] == "street_number":
         deal_with_street_number()
    if element["types"][0] == "long_name ":
         deal_with_street_long_name()