Home > Software engineering >  Destructuring nested geojson object into a list
Destructuring nested geojson object into a list

Time:10-14

I know there are many similar questions, however I could not figure out any solution up to now. So I have this object

{"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]}}]}

What I want to obtain is a new Array that looks like this:

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

How could I achieve this with destructuring?

Or is there any much better way?

CodePudding user response:

A fairly straightforward Array.map() on input.features along with some destructuring should give you the result you want:

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; }

CodePudding user response:

MDN Documentation links ...

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; }

  • Related