所以,我正在使用Google Maps API返回数据&将其放入parse.com数据库。
我将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&amp; compCont2)。
但是,结果2 comp2 / compConts2包含结果1&amp;的address_components。结果2,结果3 = 1,2,3&amp;等等。
所以,我需要一种方法,每次外部for循环操作时,它清除comp2&amp; compConts2。我试过了:
comp2 = null;
&安培;
comp2 = "";
两者似乎都不起作用打破外循环。
答案 0 :(得分:1)
您正在使用.push()
,其中comp2
和compCont2
都是数组,所以您是否尝试通过将它们设置回原来的空值来清除它们你的外comp2 = []; compCont2 = [];
循环中的数组for
?或者你可以简单地在循环中声明那些数组变量,因为你想要的每次都是一个干净的实例。