将嵌套的 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: ...}
    }
]

我如何通过解构实现这一点?

或者有更好的方法吗?

input.features 上的一个相当简单的 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; }