[英]How to get specific value from GeoJSON object
I have the below posted array of GeoJSON points.我有以下发布的 GeoJSON 点数组。 I would like to be able to access the properties of each value in the GeoJSON array separately.
我希望能够分别访问 GeoJSON 数组中每个值的属性。 For example, for the item of index 3 i would like to get
例如,对于索引 3 的项目,我想得到
"{\"type\":\"Point\",\"coordinates\":6.7368576747845,51.1141914104954]}"
, "{\"type\":\"Point\",\"coordinates\":6.7368576747845,51.1141914104954]}"
,
I posted my attempts below but it does not work and returns undefined
.我在下面发布了我的尝试,但它不起作用并返回
undefined
。 Please let me know how to access each element of the array separately so that I can get the value of the coordinates
and type
.请让我知道如何分别访问数组的每个元素,以便我可以获得
coordinates
和type
的值。
Array of GeoJSON points : GeoJSON 点数组:
[
"{\"type\":\"Point\",\"coordinates\":[6.73648711211944,51.1144539430392]}",
"{\"type\":\"Point\",\"coordinates\":[6.73628689838002,51.1141803601161]}",
"{\"type\":\"Point\",\"coordinates\":[6.73648765865954,51.1141088970156]}",
"{\"type\":\"Point\",\"coordinates\":[6.7368576747845,51.1141914104954]}",
"{\"type\":\"Point\",\"coordinates\":[6.73641043458919,51.1141123530415]}",
"{\"type\":\"Point\",\"coordinates\":[6.73655920234526,51.1144536412875]}",
"{\"type\":\"Point\",\"coordinates\":[6.73614395402386,51.1142745754592]}",
"{\"type\":\"Point\",\"coordinates\":[6.73642965248497,51.1141830888474]}",
"{\"type\":\"Point\",\"coordinates\":[6.73641750225145,51.1144305273589]}",
"{\"type\":\"Point\",\"coordinates\":[6.73671503777408,51.1141886499952]}"
]
Code代码
console.log("this.centerPointsAsGeoJSONArray[0].coordinates: ",this.centerPointsAsGeoJSONArray[0].type);
console.log("this.centerPointsAsGeoJSONArray[0].coordinates: ",this.centerPointsAsGeoJSONArray[0]['coordinates']);
console.log("this.centerPointsAsGeoJSONArray[0].coordinates: ",this.centerPointsAsGeoJSONArray[0]["coordinates"]);
It appears as if your GeoJSON points are an array of strings, rather than an array of objects.看起来您的 GeoJSON 点似乎是一个字符串数组,而不是一个对象数组。 I would suggest parsing the string as JSON first, using:
JSON.parse(this.centerPointsAsGeoJSONArray[0])
to convert to an object, and then accessing the required property.我建议首先将字符串解析为 JSON,使用:
JSON.parse(this.centerPointsAsGeoJSONArray[0])
转换为 ZA8CFDE6331BD59EB2AC96F8911C4B6,然后访问所需的属性。
Edit - Based on @Chris-G comment, using this.centerPointsAsGeoJSONArray.map(JSON.parse) is a more elegant approach, unless the original data format as strings of objects need to be preserved.编辑 - 基于@Chris-G 评论,使用 this.centerPointsAsGeoJSONArray.map(JSON.parse) 是一种更优雅的方法,除非需要保留作为对象字符串的原始数据格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.