Javascript 如何在typescript中访问geoJson坐标数组?
出于阅读原因,我缩短了以下GeoJSON输出:Javascript 如何在typescript中访问geoJson坐标数组?,javascript,arrays,angular,typescript,geojson,Javascript,Arrays,Angular,Typescript,Geojson,出于阅读原因,我缩短了以下GeoJSON输出: geoDefinition: features: Array(1) 0: geometry: coordinates: Array(1) 0: Array(5) 0: (2) [5.39014, 43.279295] 1: (2) [5.393069, 43.279249] 2: (2) [
geoDefinition:
features: Array(1)
0:
geometry:
coordinates: Array(1)
0: Array(5)
0: (2) [5.39014, 43.279295]
1: (2) [5.393069, 43.279249]
2: (2) [5.391814, 43.278421]
3: (2) [5.390709, 43.278749]
4: (2) [5.39014, 43.279295]
length: 5
我的目标是获取坐标数组并将其存储在另一个更通用的数组中。
在代码中,我写道:
this.locations.forEach(element => {
this.polygons = element.geoDefinition.features[0].geometry.coordinates;
定义功能[0]时
几何体仍未定义。我无法访问几何体->类型/坐标
坐标是数组的数组:
有人能告诉我我做错了什么吗
请注意,在定义中,功能可能是一个对象而不是数组
这意味着,要访问元素“0”而不是功能[0],应该使用
feature['0']
尝试以下操作:
这是一个有效的证明:
您确定要素是数组而不是对象吗?尝试功能[“0”]。geometry.coordinates您正在为forEach内部的同一个变量“polygons”指定。你确定这样可以吗?你能把你的json数据结构放进去,这样我们就可以看到实际对象的属性吗Feature['0']修复了我的问题。非常感谢。
const locations = [
{
geoDefinition:
{
features:
[{
geometry:
{
coordinates: [
[5.39014, 43.279295],
[5.39014, 43.279295],
[5.39014, 43.279295],
[5.39014, 43.279295],
]
}
}]
}
}
]
const newArr = []
locations.forEach(obj => {
newArr.push(...obj.geoDefinition.features[0].geometry.coordinates)
})
console.log('copied array is', newArr)