2013-11-22 111 views
0

我不明白为什么下面的行失败。无法将对象推入数组,javascript

locations.push(location); 

我想创建充满位置对象(注释,经度,纬度)的阵列(位置)。注释也是一个数组,但充满了注释对象(ID,智能区,时间戳,条目,操作)。

以下是代码片段。

var map;  
var locations = []; 

function Location() { 
    this.annotations = []; /* Array of annotations */ 
    this.latitude = null; /* Location latitude */ 
    this.longitude = null; /* Location longitude */ 
} 

function Annotation() { 
    this.ID  = null; 
    this.smartzone = null; 
    this.timestamp = null; 
    this.entry  = null; 
    this.action = null; 
} 

function initialize() { 
    var mapOptions = { 
     center: new google.maps.LatLng(42.73, -84.49), 
     zoom: 15, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 

    $.getJSON("http://www.site.com/pathto/map/json.php", function(data) { 
     $.each(data, function(key, val) { 

      //alert(val.ID); 
      var location = new Location(); 
      var annotation = new Annotation(); 

      if (locations.length === 0) { 

       annotation.ID  = val.ID; 
       annotation.smartzone = val.smartzone; 
       annotation.timestamp = val.timestamp; 
       annotation.entry  = val.entry; 
       annotation.action = val.action; 

       location.annotations.push(annotation); 

       location.latitide = val.latitude; 
       location.longitude = val.longitude; 

       locations.push(location); /* This is the line that fails */ 

      } else { 

       var locationExists = false; 

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

        if (val.latitude == locations[i].latitude && val.longitude == locations[i].longitude) { 

         locationExists = true; 
        } 
       } 

       if (locationExists) { 

        annotation.ID  = val.ID; 
        annotation.smartzone = val.smartzone; 
        annotation.timestamp = val.timestamp; 
        annotation.entry  = val.entry; 
        annotation.action = val.action; 

        locations[i].annotations.push(annotation); 

       } else { 

        annotation.ID  = val.ID; 
        annotation.smartzone = val.smartzone; 
        annotation.timestamp = val.timestamp; 
        annotation.entry  = val.entry; 
        annotation.action = val.action; 

        location.annotations.push(annotation); 

        location.latitide = val.latitude; 
        location.longitude = val.longitude; 
        //locations.push(location); /* Same problem */ 
       } 
      }   
     }); 
    }); 
} 
+0

你收到了什么错误? –

+2

你的意思是“失败”?失败如何? –

回答

0

我不知道这是正确的:

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

annotations不被任何定义。所以这个循环不应该执行。

这意味着i将永远是0当你在这里:

locations[i].annotations.push(annotation) 

其中老实说,我不知道为什么你的i计数器反正这样做,即使工作,似乎没有与locations阵列中的索引有任何关系。我认为你需要一些计数器来增加每次执行$.each()循环。

最后,而在其他情况下,您尝试使用计数器(即locations[i])以指示要添加的数组中的项目似乎奇怪的是,在某些情况下,你想将数据添加到locations使用push()。我认为你需要在这里保持一致。

对我来说,在循环逻辑流程可以更好地被这样做:

// have a means to store location hash with value for index location in locations 
// this prevents you from having to iterate over locations array for each data element to see if location already exists 
// this would be defined at the beginning along with locations 
var locationMap = {}; 


// this would take place inside your getJSON success function 
// counter for incrementing location index 
var locationCount = 0; 
$.each(...) { // iterate the data 
    // create simple hash of lat/long 
    var locationHash = val.latitude + val.longitude; 
    // determine if location already exists 
    if(locationMap[locationHash] == undefined) { // this is new location 
     var location = new Location(); 
     var location.annotations[0] = new Annotation(); 

     // not shown - populate location and annotation property values 

     // add to locations array 
     locations[locationCount] = location; 

     // add hash to location map 
     locationMap[locationHash] = locationCount; 

     // increment location counter 
     locationCount++; 
    } else { 
     var locationIndex = locationMap[locationHash]; 
     var annotation = new Annotation; 

     // not shown - populate annotation property values 

     // add new annotation to existing location 
     location[locationIndex].annotations.push(annotation); 
    } 
} 
+0

@ geschwe1尽管我认为这将解决您的问题,但我还建议,如果您可以控制正在生成的JSON数据,则应该强烈考虑在服务器端格式化数据结构,以便您可以更好地使用它。没有理由不能完全按照您需要从服务器返回数据,而无需使用javascript进行转换。我建议如果数据不经常改变,你甚至可能会有一个缓存的JSON表示,你可以从服务器向上服务器,以避免计算每个AJAX请求的数据结构 –