javascript - ¿Cómo acceder al array de coordenadas geoJson en typescript?
Tengo la siguiente salida GeoJSON abreviada por motivos de lectura:
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
Mi objetivo es obtener el arreglo de coordenadas y almacenarlo en otro arreglo más general. En el código, escribí:
this.locations.forEach(element => {
this.polygons = element.geoDefinition.features[0].geometry.coordinates;
Mientras se define feature[0],
la geometría sigue indefinida. No puedo acceder a geometry->type/coordinates.
coordinates es un Array de Arrays:
¿Alguien podría mostrarme qué hice mal, por favor?
Saludos cordiales,
Respuesta #1
Podría ser que features sea un objeto y no un arreglo en la definición.
Eso significaría que, para acceder al elemento '0' en lugar de feature[0], deberías usar feature['0'].
Respuesta #2
Prueba algo así:
Aquí hay una prueba funcional: https://stackblitz.com/edit/js-puskp8
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)

