Mr.KGK Mr.KGK - 1 year ago 132
Javascript Question

For Each loop ---> For loop

I am having a error that states that data.forEach is not a function. The code is:

function getProperGeojsonFormat(data) {
isoGeojson = {"type": "FeatureCollection", "features": []};

console.log("After getProperGeojsonFormat function")

data.forEach(function(element, index) {
isoGeojson.features[index] = {};
isoGeojson.features[index].type = 'Feature';
isoGeojson.features[index].properties =;
isoGeojson.features[index].geometry = {};
isoGeojson.features[index].geometry.coordinates = [];
isoGeojson.features[index].geometry.type = 'MultiPolygon';

element.geometry.geometries.forEach(function(el) {
$rootScope.$broadcast('isochrones', {isoGeom: isoGeojson});


The error I am getting is:

enter image description here

When I console log data:

When I console log data

Answer Source

data is an object. It looks like you want to loop over the features array within that object, so do:

data.features.forEach(function(element, index) {
    isoGeojson.features[index] = {
        type: 'Feature',
        geometry: {
            type: 'MultiPolygon',
            coordinates: element.coordinates.slice()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download