访问未命名对象的属性(在对象内)

时间:2015-01-26 17:21:43

标签: javascript jquery

我使用Javascript / JQuery来使用Google Web服务,并且可以毫无问题地检索JSON结果,将它们存储在var(名为searchResults)中。在这个例子中,我只是将我想要显示的所有信息从对象(address_components的每个子对象中的long_name和lat& lng值)添加到字符串var(displayTxt)。

但是,结果中有许多未命名的对象,我找不到一种简单的方法来定位它们。我想使用标准的object.property语法,尽管如果对象没有被命名(并且不止一个),那是不可能的。我已经使用嵌入式$ .each循环来循环遍历每个连续的对象,这个解决方案确实有效,但我不认为它非常整洁。所以我的问题是:是否有更好的方法来实现相同的结果?

$( "#displayBtn" ).click(function()
 {
    var displayTxt = "";
    if (searchResults.results)
        {

            $.each(searchResults.results, function (k,v)
                   {
                    displayTxt +=( "Result ID: " + k + "<br>");
                    $.each(v,function(a,b)
                           {
                            if(a=="address_components")
                                { 
                                $.each(b,function(c,d)
                                       {
                                        displayTxt += (d.long_name + "<br>")
                                        })
                                }
                            })
                    displayTxt+= "lat:" + v.geometry.location.lat +"<br>";
                    displayTxt+= "lng:" + v.geometry.location.lng +"<br><br>";
                   }
                  )

        };
     $('#resultsDisplay').html(displayTxt); 
});

这里是searchResults对象:

{
"results" : [
  {
     "address_components" : [
        {
           "long_name" : "Reading",
           "short_name" : "Reading",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Exeter",
           "short_name" : "Exeter",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Berks County",
           "short_name" : "Berks County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Pennsylvania",
           "short_name" : "PA",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Reading, PA, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 40.371134,
              "lng" : -75.890512
           },
           "southwest" : {
              "lat" : 40.296401,
              "lng" : -75.9598731
           }
        },
        "location" : {
           "lat" : 40.3356483,
           "lng" : -75.9268747
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 40.371134,
              "lng" : -75.890512
           },
           "southwest" : {
              "lat" : 40.296401,
              "lng" : -75.9598731
           }
        }
     },
     "types" : [ "locality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Reading",
           "short_name" : "Reading",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Reading",
           "short_name" : "Reading",
           "types" : [ "postal_town" ]
        },
        {
           "long_name" : "Reading",
           "short_name" : "Reading",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "England",
           "short_name" : "England",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Reading, Reading, Reading, UK",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 51.4931339,
              "lng" : -0.9284944000000001
           },
           "southwest" : {
              "lat" : 51.4097795,
              "lng" : -1.0636011
           }
        },
        "location" : {
           "lat" : 51.4542645,
           "lng" : -0.9781303
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 51.4931339,
              "lng" : -0.9284944000000001
           },
           "southwest" : {
              "lat" : 51.4097795,
              "lng" : -1.0636011
           }
        }
     },
     "types" : [ "locality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Reading",
           "short_name" : "Reading",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Middlesex County",
           "short_name" : "Middlesex County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Massachusetts",
           "short_name" : "MA",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Reading, MA, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 42.566432,
              "lng" : -71.07103499999999
           },
           "southwest" : {
              "lat" : 42.5006449,
              "lng" : -71.1361789
           }
        },
        "location" : {
           "lat" : 42.5256563,
           "lng" : -71.0952891
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 42.566432,
              "lng" : -71.07103499999999
           },
           "southwest" : {
              "lat" : 42.5006449,
              "lng" : -71.1361789
           }
        }
     },
     "types" : [ "locality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Reading",
           "short_name" : "Reading",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Hamilton County",
           "short_name" : "Hamilton County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Ohio",
           "short_name" : "OH",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Reading, OH, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 39.240921,
              "lng" : -84.407549
           },
           "southwest" : {
              "lat" : 39.2038819,
              "lng" : -84.454735
           }
        },
        "location" : {
           "lat" : 39.2236694,
           "lng" : -84.44216410000001
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 39.240921,
              "lng" : -84.407549
           },
           "southwest" : {
              "lat" : 39.2038819,
              "lng" : -84.454735
           }
        }
     },
     "types" : [ "locality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Reading",
           "short_name" : "Reading",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Windsor County",
           "short_name" : "Windsor County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Vermont",
           "short_name" : "VT",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Reading, VT, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 43.561169,
              "lng" : -72.53007579999999
           },
           "southwest" : {
              "lat" : 43.4440849,
              "lng" : -72.66064299999999
           }
        },
        "location" : {
           "lat" : 43.494167,
           "lng" : -72.59638900000002
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 43.561169,
              "lng" : -72.53007579999999
           },
           "southwest" : {
              "lat" : 43.4440849,
              "lng" : -72.66064299999999
           }
        }
     },
     "types" : [ "locality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Reading",
           "short_name" : "Reading",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Summit Lake",
           "short_name" : "Summit Lake",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Nobles County",
           "short_name" : "Nobles County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Minnesota",
           "short_name" : "MN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "56165",
           "short_name" : "56165",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "Reading, MN 56165, USA",
     "geometry" : {
        "location" : {
           "lat" : 43.7035752,
           "lng" : -95.7130676
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 43.70492418029149,
              "lng" : -95.71171861970849
           },
           "southwest" : {
              "lat" : 43.70222621970849,
              "lng" : -95.71441658029151
           }
        }
     },
     "types" : [ "locality", "political" ]
  }
],
"status" : "OK"
}

1 个答案:

答案 0 :(得分:1)

看起来好像有一个对象数组,它们没有名称(它们没有分配给变量,只是数组的一部分)。不过,你确实有一些不同的选项来迭代数组。

经典迭代器是一个使用长度的for循环,例如:

var data = [{value: 1}, {value: 2}, {value: 3}, {value: 4}, {value: 5}];

for (var i = 0; i < data.length; ++i) {
  console.log(data[i].value);
}

这将记录遇到的每个项目,并使用数组索引来访问它们。项目是什么并不重要,只需更改数组中的逻辑即可。与$.each一样,您可以嵌套这些循环(只需确保使用不同的变量)。

如果您可以访问某些新的JS功能,则会在阵列中添加forEach方法,您可以这样做:

var data = [{value: 1}, {value: 2}, {value: 3}, {value: 4}, {value: 5}];

data.forEach(function(it) {
  console.log(it.value);
});

这更像$.each,但是内置在数组上,并且在迭代DOM元素等时没有相同的语义。再一次,你可以嵌套这些,但没有办法担心。如果可以的话,这是推荐的方式。

无论哪种方式,一旦你有了这个项目(通过从数组中访问它),你就有一个“名称”(它被分配给一个变量)你可以使用普通的对象访问语法来获取属性对象。

如果要将数据转换为新数组或将元素组合成单个返回值,您可以查看类似mapreduce的方法,现在可用于数组(以前来自Underscorelodash)等库。