• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

145
Vistas
Destructuring nested geojson object into a list

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?

about 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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

about 3 years ago · Juan Pablo Isaza Denunciar

0

MDN Documentation links ...

  • Destructuring assignment
    • Object destructuring
    • Array destructuring
    • Nested object and array destructuring

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

about 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda