0

I have an issue and this is not something that I have ever used before.

This is a snippet of the code.

//Defined at top of file
var ships_array = [];

//Function later on

    function refreshJSON(){
    $.getJSON("scripts/getJSON.php", function(json){

      if(json){
        $.each(shiplist,function(i,item){
            $.each(item,function(j,subitem){
                var mmsi = subitem['mmsi'];
                ships_array[mmsi].lat = subitem['lat'];
                ships_array[mmsi].lon = subitem['lon']; 

            });
        });
     } 

    });

    }

The error I am getting is

ships_array[mmsi] is undefined [Break On This Error] ships_array[mmsi].lat = subitem['lat'];

Is this the right way to use an associative array? I have seen an example similar to this but I am going wrong somewhere! At the end of it I want an array of Google Maps markers!

2 Answers 2

3

you just need to initialize the value at the hash key first

var mmsi = subitem['mmsi'];
ships_array[mmsi] = ships_array[mmsi] || {};
ships_array[mmsi].lat = subitem['lat'];
ships_array[mmsi].lon = subitem['lon'];
Sign up to request clarification or add additional context in comments.

8 Comments

Works great,I don't understand the second line, can you point me to a reference or explain please? Will accept when it lets me as it works!
The || symbol is or, when used in assignments it means if the first item evaluates to false assign the next one. So for x = false || 'foo' || 'bar'; x gets the value 'foo'.
The second line takes a logical OR between the array element - ships_array[mssi] and an empty object literal {}. The former to make sure that an existing object is not overwritten, the latter to ensure that an empty object exists.
Sure. The problem is that if ships_array @ mmsi is undefined, you can't possibly set .lat or .lon on it - that would be the same as trying to do (undefined).lat = 'test'. The second line is a common javascript pattern for assigning default values. ships_array[mmsi] || {} will return either ships_array[mmsi] if it is defined, or {} if it is not. This is then used to set ships_array[mmsi]. An equivalent alternative second line would be if(!ships_array[mmsi]){ships_array[mmsi]={};}. Does that make it more clear?
Yes spot on, just getting to grips with JavaScript!
|
1

You can change your code like this:

//Defined at top of file
var ships_array = [];

//Function later on

    function refreshJSON(){
    $.getJSON("scripts/getJSON.php", function(json){

      if(json){
        $.each(shiplist,function(i,item){
            $.each(item,function(j,subitem){
                var mmsi = subitem['mmsi'];
                ships_array[mmsi] = {lat: subitem['lat'], lon:subitem['lon']};
            });
        });
     } 

    });

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.