Search code examples
javascriptarraysjsonmultidimensional-arraygeojson

From geoJSON to Array


The real issue was that i could not seem to be able to set the value inside the coordinates.

The start of the JSON looks something like this.

{  
    "path":{  
        "type":"FeatureCollection",
        "features":[  
            {  
                "geometry":{  
                    "type":"LineString",
                    "coordinates":[  
                        [  
                            10.4048129700337,
                            63.416688738540316
                        ],
                        [  
                            10.404757623420094,
                            63.4167327334674
                        ]
                    ]
                },
                "type":"Feature",
                "id":85624,
                "properties":{  
                    "flags":[  

                    ],
                    "z":2.0,
                    "m":5.62957707088913e-06,
                    "target":85621,
                    "buildingId":43
                }
            }

When I ran the code it gave me

"Uncaught TypeError: Cannot set property '0' of undefined" on remakeObj[i].coordinates[j][k] = coordinates[j][k];

This is now solved. I solved it this way.

parseToObjects: function (data) {
    var remakeObj = [];
    var features = data.path.features;
    for (var i in features) {
        remakeObj[i] = {
            flooring: "",
            meters: "",
            coordinates: []
        };

        var coordinates = features[i].geometry.coordinates;

        for (var j in coordinates) {
            remakeObj[i].coordinates[j] = [];
            for (var k in coordinates[j]) {

                remakeObj[i].coordinates[j][k] = coordinates[j][k];
                remakeObj[i].flooring = features[i].properties.z;
                remakeObj[i].meters = features[i].properties.m;

            }
        }

    }
    console.log(remakeObj);
    return remakeObj;
}

Solution

  • Arrays in JavaScript are practically hash tables (think of it like buckets that contain values and a key in order to access it in a way) and not serially number-indexed only arrays.

    Although numeric indexes as keys are allowed, the array can contain as well as keys of other types (string type etc.).

    Therefore, the index 0 will not return the first element of the array, but the value element stored myArray[0], if any (in your case, there is not any and this is why you get the error).

    You need to iterate the array using the keys set in order to access all of the values serially:

    for (var key in myArray) {
        var y = myArray[key]; //Index it like this, use it similarly.
    }
    

    UPDATE: This is what you could possibly do (Warning: Untested code):

    for (var featuresKey in features) {
        remakeObj[featuresKey] = [features.length];
        var coordinates = features[featuresKey].geometry.coordinates;
        remakeObj[featuresKey].coordinates = new Array(coordinates.length);
    
        for (var coordinatesKey in coordinates) {
    
            for (var innerCoordinatesKey in coordinates[coordinatesKey]) {
                remakeObj[featuresKey].coordinates[coordinatesKey][innerCoordinatesKey] =
                    coordinates[coordinatesKey][innerCoordinatesKey];
                remakeObj[featuresKey].flooring = features[featuresKey].properties.z;
                remakeObj[featuresKey].meters = features[featuresKey].properties.m;
    
    
            }
    
        }
    
    }