从数组中获取子类

时间:2015-10-17 22:31:12

标签: javascript jquery json

我正在学习如何编写我正在尝试构建的项目。因此,这是我使用Google Maps API执行的一些JavaScript代码片段:

for (i = 0; i < results.length; i++) {


    console.log("Formatted Address: "+ results[i].formatted_address + "\n" + 
        "Geometry: "+ results[i].geometry.location + "\n" + 
        "Types: "+ results[i].types + "\n" +
        results[i].address_components[0].types + ": " + results[i].address_components[0].long_name + "\n" +
        results[i].address_components[1].types + ": " + results[i].address_components[1].long_name + "\n" +
        results[i].address_components[2].types + ": " + results[i].address_components[2].long_name + "\n" +
        results[i].address_components[3].types + ": " + results[i].address_components[3].long_name + "\n" +
        results[i].address_components[4].types + ": " + results[i].address_components[4].long_name + "\n" +
        results[i].address_components[5].types + ": " + results[i].address_components[5].long_name + "\n" +
        results[i].address_components[6].types + ": " + results[i].address_components[6].long_name + "\n" +
        results[i].address_components[7].types + ": " + results[i].address_components[7].long_name + "\n" +
        results[i].address_components[8].types + ": " + results[i].address_components[8].long_name + "\n" +
        results[i].address_components[9].types + ": " + results[i].address_components[9].long_name
    );

    formattedAddress = results[i].formatted_address;
    coordinates = results[i].geometry.location;
    types = results[i].types;
    // component = results[i].address_components[0].types;

    no = i+1;

    output += "<li>";
    output += "<p><i>"+ no +"</i></p>"
    output += "<p><b>"+ formattedAddress +"</b></p>";
    output += "<p>"+ coordinates +"</p>";
    output += "<p>"+ types +"</p>";
    output += "</li>";

    //console.log("results for "+ [i] + " :" + output);
    $("#list-locations").html(output);
}

我正在尝试阅读&amp;输出地址组件(.types&amp; .long_name),根据搜索项可以改变长度。某些搜索字词只返回1种类型&amp; long_name字段,而其他搜索字词可以返回7或8。

我最终希望将它添加到我的输出变量中。

以下是JSON Return的示例:

{
  "types":["locality","political"],
  "formatted_address":"Winnetka, IL, USA",
  "address_components":[{
    "long_name":"Winnetka",
    "short_name":"Winnetka",
    "types":["locality","political"]
  },{
    "long_name":"Illinois",
    "short_name":"IL",
    "types":["administrative_area_level_1","political"]
  },{
    "long_name":"United States",
    "short_name":"US",
    "types":["country","political"]
  }],
  "geometry":{
    "location":[ -87.7417070, 42.1083080],
    "location_type":"APPROXIMATE"
  },
  "place_id": "ChIJW8Va5TnED4gRY91Ng47qy3Q"
}

在此示例中,address_components[2].long_name将返回&#34;美国&#34;,而address_components[3].long_name将返回undefined

如何添加计数器,以便...... address_components[j].long_nam e是no的长度。特定搜索字词中的字段(j是该数字)?

1 个答案:

答案 0 :(得分:0)

好的,所以我通过执行以下操作解决了问题:

for (i = 0; i < results.length; i++) {

    formattedAddress = results[i].formatted_address;
    coordinates = results[i].geometry.location;
    types = results[i].types;

    no = i+1;

    output += "<li>";
    output += "<H1><i>"+ no +"</i></H1>"
    output += "<p><b>"+ formattedAddress +"</b></p>";
    output += "<p>"+ coordinates +"</p>";
    output += "<p>"+ types +"</p>";
    for (j = 0; j < results[i].address_components.length; j++) {
        comp = results[i].address_components[j].types;
        compCont = results[i].address_components[j].long_name;
        output += "<p>"+ comp +": " + compCont +"</p>";
    }
    output += "</li>";

    $("#list-locations").html(output);
}

根据@localghost评论,如果我小于或等于我需要-1