簡體   English   中英

將嵌套的 geojson 對象解構為列表

[英]Destructuring nested geojson object into a list

我知道有很多類似的問題,但是到目前為止我無法找到任何解決方案。 所以我有這個對象

{"type":"FeatureCollection","name":"zentroide_bezirke_4326","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},"features":[{"type":"Feature","properties":{"fid":2,"id":"BEZIRKSGRENZEOGD.10890","NAMEK":"Josefstadt","BEZNR":8,"BEZ_RZ":"VIII","NAMEK_NUM":"8., Josefstadt","NAMEK_RZ":"VIII. Josefstadt","NAMEG":"JOSEFSTADT","LABEL":"VIII.","BEZ":"08","DISTRICT_CODE":1080,"STATAUSTRIA_BEZ_CODE":908,"STATAUSTRIA_GEM_CODE":90801,"FLAECHE":1089945.694,"UMFANG":4170.3,"AKT_TIMESTAMP":"2021-09-13","SE_SDO_ROWID":10890,"SE_ANNO_CAD_DATA":null},"geometry":{"type":"Point","coordinates":[16.347822689187193,48.211029313540855]}},{"type":"Feature","properties":{"fid":3,"id":"BEZIRKSGRENZEOGD.10891","NAMEK":"Innere Stadt","BEZNR":1,"BEZ_RZ":"I","NAMEK_NUM":"1., Innere Stadt","NAMEK_RZ":"I. Innere Stadt","NAMEG":"INNERE STADT","LABEL":"I.","BEZ":"01","DISTRICT_CODE":1010,"STATAUSTRIA_BEZ_CODE":901,"STATAUSTRIA_GEM_CODE":90101,"FLAECHE":2868773.8207,"UMFANG":6972.75,"AKT_TIMESTAMP":"2021-09-13","SE_SDO_ROWID":10891,"SE_ANNO_CAD_DATA":null},"geometry":{"type":"Point","coordinates":[16.36939878396175,48.208360983479615]}}]}

我想要獲得的是一個新的數組,如下所示:

let data = [
    {
        NAMEK: ...
        coordinates: {lat: ..., lng: ...}
    },
    {
        NAMEK: ...
        coordinates: {lat: ..., lng: ...}
    }
]

我如何通過解構實現這一目標?

或者有沒有更好的方法?

Array.map()上的一個相當簡單的Array.map()以及一些解構應該會給你你想要的結果:

 const input = {"type":"FeatureCollection","name":"zentroide_bezirke_4326","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},"features":[{"type":"Feature","properties":{"fid":2,"id":"BEZIRKSGRENZEOGD.10890","NAMEK":"Josefstadt","BEZNR":8,"BEZ_RZ":"VIII","NAMEK_NUM":"8., Josefstadt","NAMEK_RZ":"VIII. Josefstadt","NAMEG":"JOSEFSTADT","LABEL":"VIII.","BEZ":"08","DISTRICT_CODE":1080,"STATAUSTRIA_BEZ_CODE":908,"STATAUSTRIA_GEM_CODE":90801,"FLAECHE":1089945.694,"UMFANG":4170.3,"AKT_TIMESTAMP":"2021-09-13","SE_SDO_ROWID":10890,"SE_ANNO_CAD_DATA":null},"geometry":{"type":"Point","coordinates":[16.347822689187193,48.211029313540855]}},{"type":"Feature","properties":{"fid":3,"id":"BEZIRKSGRENZEOGD.10891","NAMEK":"Innere Stadt","BEZNR":1,"BEZ_RZ":"I","NAMEK_NUM":"1., Innere Stadt","NAMEK_RZ":"I. Innere Stadt","NAMEG":"INNERE STADT","LABEL":"I.","BEZ":"01","DISTRICT_CODE":1010,"STATAUSTRIA_BEZ_CODE":901,"STATAUSTRIA_GEM_CODE":90101,"FLAECHE":2868773.8207,"UMFANG":6972.75,"AKT_TIMESTAMP":"2021-09-13","SE_SDO_ROWID":10891,"SE_ANNO_CAD_DATA":null},"geometry":{"type":"Point","coordinates":[16.36939878396175,48.208360983479615]}}]}; const result = input.features.map(({ properties: { NAMEK }, geometry: { coordinates: [lat,lng] } }) => { return { NAMEK, coordinates: { lat, lng } }; }); console.log('Result:', result)
 .as-console-wrapper { max-height: 100% !important; top: 0; }

MDN 文檔鏈接...

 console.log(({ "type": "FeatureCollection", "name": "zentroide_bezirke_4326", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84", }, }, "features": [{ "type": "Feature", "properties": { "fid": 2, "id": "BEZIRKSGRENZEOGD.10890", "NAMEK": "Josefstadt", "BEZNR": 8, "BEZ_RZ": "VIII", "NAMEK_NUM": "8., Josefstadt", "NAMEK_RZ": "VIII. Josefstadt", "NAMEG": "JOSEFSTADT", "LABEL": "VIII.", "BEZ": "08", "DISTRICT_CODE": 1080, "STATAUSTRIA_BEZ_CODE": 908, "STATAUSTRIA_GEM_CODE": 90801, "FLAECHE": 1089945.694, "UMFANG": 4170.3, "AKT_TIMESTAMP": "2021-09-13", "SE_SDO_ROWID": 10890, "SE_ANNO_CAD_DATA": null, }, "geometry": { "type": "Point", "coordinates": [16.347822689187193, 48.211029313540855], }, }, { "type": "Feature", "properties": { "fid": 3, "id": "BEZIRKSGRENZEOGD.10891", "NAMEK": "Innere Stadt", "BEZNR": 1, "BEZ_RZ": "I", "NAMEK_NUM": "1., Innere Stadt", "NAMEK_RZ": "I. Innere Stadt", "NAMEG": "INNERE STADT", "LABEL": "I.", "BEZ": "01", "DISTRICT_CODE": 1010, "STATAUSTRIA_BEZ_CODE": 901, "STATAUSTRIA_GEM_CODE": 90101, "FLAECHE": 2868773.8207, "UMFANG": 6972.75, "AKT_TIMESTAMP": "2021-09-13", "SE_SDO_ROWID": 10891, "SE_ANNO_CAD_DATA": null, }, "geometry": { "type": "Point", "coordinates": [16.36939878396175, 48.208360983479615], }, }] }) .features .map(({ properties: { NAMEK }, geometry: { coordinates: [lat, lng] }, }) => ({ NAMEK, coordinates: { lat, lng }, })) );
 .as-console-wrapper { min-height: 100%!important; top: 0; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM