2016-06-23 16 views
-4

data.forEachが関数ではないというエラーがあります。コードは次のとおりです。各ループについて---> Forループ

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

    console.log("After getProperGeojsonFormat function") 
    console.log(data) 
    console.log("") 

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

     element.geometry.geometries.forEach(function(el) { 
      isoGeojson.features[index].geometry.coordinates.push(el.coordinates); 
     }); 
    }); 
    $rootScope.$broadcast('isochrones', {isoGeom: isoGeojson}); 



} 

私は取得していますエラーは次のとおりです。

enter image description here

私はデータログコンソールとき:アレイ上ではなく、オブジェクトの

When I console log data

+4

データが配列かどうかによって異なります。 –

+1

[mcve]は素晴らしいでしょう! –

+1

彼らはすでにあなたを助けました – zerkms

答えて

0

dataが対象です。オブジェクト内の配列をループしたいので、次のようにします。

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

ヒントがdata.features.forEachを変更していただきありがとうございます。今私は次の問題があります毎回 –

+0

謝罪しますが、そこに戻って –

+0

質問に答えを入れないでください。私は元の質問を元に戻しました。 – Barmar

0

forEach作品。ここではdataが対象と思われます。

これを代わりに使用してください。

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

    element.geometry.geometries.forEach(function(el) { 
     isoGeojson.features[index].geometry.coordinates.push(el.coordinates); 
    }); 
}); 

Object.keysオブジェクトのキーから配列を作成します。これらのキーを繰り返し処理し、関連する値をフェッチすることができます。 このアプローチは、どのオブジェクトでも機能します。

関連する問題