填充javascript数组时出错

填充javascript数组时出错,javascript,jquery,Javascript,Jquery,下面是我的简单javascript代码。在这种情况下,我在填充位置数组时遇到错误“不能设置未定义的属性” var locations = []; for (var i=0;i<center_add.length;i++) { var geocoder = new google.maps.Geocoder(); // center_add[i].toString(); // alert(center_add[i].toString()); geocoder.geocode(

下面是我的简单javascript代码。在这种情况下,我在填充位置数组时遇到错误“不能设置未定义的属性”

var locations = [];
for (var i=0;i<center_add.length;i++)
{
  var geocoder = new google.maps.Geocoder();
  // center_add[i].toString();
  // alert(center_add[i].toString());
  geocoder.geocode( { 'address': center_add[i].toString()}, function(results, status) {
      locations[i]=[];
    if (status == google.maps.GeocoderStatus.OK) {
      locations[i][0] = center_add[i].toString();
      // Uncought TypeError="Can not set property '0' of undefined.
      locations[i][1]= results[0].geometry.location.lat();
      locations[i][2]= results[0].geometry.location.lng();
    } 
  }); 
}
var位置=[];

对于(var i=0;icheck
typeof=='undefined'
在访问数组元素之前,您缺少
位置[i]=[];
@IgorJerosimić仍然显示相同的错误…..您确定是相同的错误,因为这似乎不太可能吗?无论如何,尝试在我们可以检查的环境中重现错误(JsFiddle、live site等)以便我们能提供更多帮助。@Tibos得到解决。我在某些位置丢失了s…。谢谢