2015-10-20 79 views
0

因此,我将Google Maps API返回数据&放置到parse.com数据库中。在每个通道中清除For循环内容的循环内容

我将JSON返回的address_component部分存储在数组中。

整体JSON返回可能包含1个或多个位置(具体取决于所使用的搜索术语)。这里是我的代码:

if (status == google.maps.GeocoderStatus.OK) { 

           var output = ""; 
           var formattedAddress; 
           var lat; 
           var lng; 
           var types; 
           var comp; 
           var compCont; 
           var comp2 = []; 
           var compCont2 = []; 
           var i; 
           var j; 
           var k; 
           var l; 

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

            formattedAddress = results[i].formatted_address; 
            coordinates = results[i].geometry.location; 
            lat = results[i].geometry.location.lat(); 
            lng = results[i].geometry.location.lng(); 

            types = results[i].types; 

            var no = 1+i; 

            output += "<li>"; 
            output += "<H1><i>"+ no +"</i></H1>" 
            output += "<p><b>"+ formattedAddress +"</b></p>"; 
            output += "<p>"+ "lat: " + lat + ", lng: "+ lng +"</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>"; 


            for (k = 0; k < results[i].address_components.length; k++) { 
              comp2.push(results[i].address_components[k].types); 
              compCont2.push(results[i].address_components[k].long_name); 
            } 

            var Searches = Parse.Object.extend("Searches"); 
            var searches = new Searches(); 

            searches.set("Searched", name); 
            searches.set("Returned_Address_Qty", no); 
            searches.set("Address", formattedAddress); 
            searches.set("Latitude", lat); 
            searches.set("Longitude", lng); 
            searches.set("Section", comp2); 
            searches.set("Section_Content", compCont2); 

            searches.save(null, { 
              success: function(searches) { 
              // Execute any logic that should take place after the object is saved. 
              // alert('New object created with objectId: ' + searches.id); 
              }, 
              error: function(searches, error) { 
              // Execute any logic that should take place if the save fails. 
              // error is a Parse.Error with an error code and message. 
              alert('Failed to create new object, with error code: ' + error.message); 
              } 
            }); 

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

           } 
         } else { 
           alert("Geocode was not successful for the following reason: " + status); 
         } 

因此,如果搜索查询的结果超过1,就会出现问题。按照预期,发送给Parse的第一条记录包含数组中的address_components(comp2 & compCont2)。

但是,结果2 comp2/compConts2包含结果1的地址_部分1 &结果2,结果3 = 1,2,3 &等等。

所以,我需要一种方式,每次外循环操作,它清除comp2 & compConts2。我曾尝试:

comp2 = null; 

&

comp2 = ""; 

似乎都不虽然&休息外for循环工作。

回答

1

您使用.push()这既期待和comp2compCont2是数组,所以你尝试将其设定回原来的是一个空数组comp2 = []; compCont2 = [];你的外for循环中值清除它们?或者您可以简单地在循环内声明这些数组变量,因为每次您想要的都是干净的实例。

+0

当然!!!!感谢那。我重新声明了数组'']' –