无法将对象推入数组,javascript

时间:2013-11-22 01:13:26

标签: javascript arrays multidimensional-array

我不明白为什么以下行失败。

    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 */
                }
            }           
        });
    });
}

1 个答案:

答案 0 :(得分:0)

我不知道这是对的:

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

annotations未在任何地方定义。所以这个循环永远不应该执行。

这意味着当您到达时i始终为0

locations[i].annotations.push(annotation)

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

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

对我来说,循环中的逻辑流程最好这样做:

// 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);
     }
}