【发布时间】:2016-06-23 01:42:19
【问题描述】:
我遇到一个错误,指出 data.forEach 不是函数。代码是:
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});
}
我得到的错误是:
当我控制台日志数据时:
【问题讨论】:
-
看data是不是数组
-
他们已经帮你了
-
@SamuelToh 我的回复是 JSON
-
JS 中的 JSON 是一个字符串。 JS 中的字符串没有
forEach方法。
标签: javascript for-loop