JavaScript - 如何使用Map而不是For Loop

时间:2014-01-31 19:33:36

标签: javascript

我想在这个例子中使用map而不是for循环。 我有一个包含数据的CSV文件

CSV file
address,type,building,geometry
"This","is","an","example"
"This","is","an","example"
"This","is","an","example"
"This","is","an","example"
"This","is","an","example"
"This","is","an","example"
"This","is","an","example"

var geojsonFeature;

var globalData= data.map(function(d){return JSON.parse(d.geometry);});
var buildingData= data.map(function(d){return JSON.parse(d.type);});


for (i=0;i<globalData.legnth;i++)
{
geojsonFeature = {
        "type": "Feature",
        "properties": {
            "name": buildingData[i]
        },
        "geometry": {
            "type": "MultiPolygon",
            "coordinates": globalData[i].coordinates
        }
    };

 listGeoData.push(geojsonFeature)

}

我想用地图替换For-Loop以这种方式获得“listGeoData”。

1 个答案:

答案 0 :(得分:0)

这假设listGeoDatafor循环之前为空。

var listGeoData = globalData.map(function(data,i) {
  return {
    "type": "Feature",
    "properties": {
      "name": buildingData[i]
    },
    "geometry": {
      "type": "MultiPolygon",
      "coordinates": data.coordinates
    }
  };
});