簡體   English   中英

無法將對象推入數組,JavaScript

[英]Failing to push object into array, 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 */
                }
            }           
        });
    });
}

我不知道這是正確的:

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

annotations未在任何地方定義。 因此此循環永遠都不會執行。

這意味着當您到達此處時, i將始終為0

locations[i].annotations.push(annotation)

老實說,我不知道您為什么仍要這樣做,因為i計數器即使工作也似乎與locations數組中的索引沒有任何關系。 我認為您需要在每次執行$.each()循環時增加一些計數器。

最后,在某些情況下要使用push()將數據添加到locations而在其他情況下要嘗試使用計數器(即locations[i] )來指示要在數組中添加項目的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);
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM