0

My code has two functions that pass arrays to another function. The first time I execute this, it works, the second time it doesn't.

First time (snippet of a larger function):

for (m = 0; m < totalAnimals; m++) {
    ids = mergedIDs[m];
    file = "data/" + ids + ".geojson";
    coords = arrayArray[3 * m];
    time = arrayArray[3 * m + 1];
    props = arrayArray[3 * m + 2];

    $.getJSON(file).done(function (data) { 
        $(data).each(function () {
            coords.push(data.geometry.coordinates); 
            time.push(data.properties.time);
            id = data.properties.id;
            species = data.properties.species;
            sex = data.properties.sex;
            props.push(id, species, sex);
        });
    }).done(function () {
        makeTrack(coords, time, props);
        coords = []; // clearing in between rounds
        time = [];
        props = [];
    }).fail(function () {
        console.log(ids + "multipointCoords not populated");
    });

}

The code above successfully passes the three arrays (coords, time, props) to the makeTrack function:

    function makeTrack(multipointCoords, tracksTime, properties) {

        // parse properties array to assign to features
        var id = properties[0],
            species = properties[1],
            sex = properties[2];

        // setup OpenLayers structure and style assignment
        var trackSource = new ol.source.Vector(); // create an empty source instance

        var lineString = new ol.geom.LineString([ // create a linestring geometry from the GeoJSON data for the tracks
            ol.proj.fromLonLat(multipointCoords[0][0])
        ]);

        var icon = new ol.geom.Point( // create a point geometry from the GeoJSON data for the header icons
            ol.proj.fromLonLat(multipointCoords[0][0])
        );

etc. Everything is great so far. Later in that function I call

       segmentConstruction(multipointCoords);

and then

      function segmentConstruction(multipointCoords) {
            length = multipointCoords[0].length;

            if (i < length) {
                var timer;

                prevCoord = ol.proj.fromLonLat(multipointCoords[0][i - 1]);
                currCoord = ol.proj.fromLonLat(multipointCoords[0][i]);
                ...

I am getting the error:

TypeError: multipointCoords[0] is undefined

I did read this Javascript passing array as parameter to function, but I do not understand the difference between passing references and passing values.

According to Passing an array as parameter in JavaScript, arrays can be passed.

EDIT the arrays are two-dimensional because they are arrays of [lon,lat].

9
  • 2
    The dimensionality is irrelevant. But you're doing async stuff inside a loop that will not stop just because you're doing async stuff inside it. Commented Oct 30, 2015 at 20:18
  • If all of your variables inside the for loop were defined with let, the problem would likely go away, however that's not supported by all browsers. Commented Oct 30, 2015 at 20:20
  • The problem here is your callback passed to .done closes around the coords variable, and passes the value of it to the next function, which is a reference to an array that was defined in the last iteration of the for loop. Then, each time .done callback gets called, for each iteration, they're acting upon the same array, the one created in the last iteration. This would surely cause unexpected results, possibly even the error you've come across. Commented Oct 30, 2015 at 20:26
  • @DaveNewton What do you mean by "closes" around the coords variable? I am not getting any errors or unexpected results in the makeTrack function, except perhaps the issue I present here: that even though I can use the coords/multipointCoords array in the makeTrack function, I can't pass it from there on to another function (segmentConstruction). Commented Oct 30, 2015 at 20:33
  • eh, you can ingore the "closes" around the coords var statement, it's really not important. What is important is that it's accessing the value of that variable, after the for loop as completed. It will contain the array generated on the last iteration, for all done callbacks. each callback won't have it's own array. Commented Oct 30, 2015 at 20:35

0

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.