0

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?

Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Lenn
  • 1,283
  • 7
  • 20
  • 1
    Can you please elaborate, What structure do you need exactly? What does "NAMEK" represent? – nir segev Oct 13 '21 at 07:40
  • 1
    If the structure of an object is important for the task, then it's a terrible idea to mash it all in one line. Use proper formatting for the input. If that would take up a whole page then it's not a minimal example. and needs to be stripped from unnecessary properties. – Andreas Oct 13 '21 at 07:40
  • You cannot "destructure" an object into an array directly: [Destructure object into an array](https://stackoverflow.com/questions/58508433/destructure-object-into-an-array), [Destructure object properties into array values](https://stackoverflow.com/questions/48709596/destructure-object-properties-into-array-values), ... – Andreas Oct 13 '21 at 07:43
  • 1
    Would you also be able to provide your attempt and highlight what you're having trouble with specifically? – Nick Parsons Oct 13 '21 at 07:46
  • @Lenn .. From all the provided solutions, are there any questions left? – Peter Seliger Oct 16 '21 at 08:26
  • I will, I will. Sorryyy... I was on holiday and just came back... I want to read the answer with calm. I am sorry for the delay:/ – Lenn Oct 19 '21 at 11:46

2 Answers2

2

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; }
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
1

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; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • thank you very much!! Sorry again for the late answer, but this helped a lot to clear things in my head – Lenn Oct 20 '21 at 06:45
  • Just one question: If I were to extract the value of the `NAMEK` property of the first element in features into a newly named variable `name`, i tried this ``` let s = input.features[1]; console.log(s) let {properties: { NAMEK }: name} = s; ``` But this is really wrong. Sorry this destructing really confuses me... – Lenn Oct 20 '21 at 07:09
  • 1
    For the described case one would destructure the object like `let { properties: { NAMEK: name } } = s;` which is equivalent to `let name = s.properties.NAMEK` and shows that one only initializes `name`. One then would assign it as entry to another object like `{ name }`. But one also can keep the former destructuring. Then one would write `{ name: NAMEK }`. – Peter Seliger Oct 20 '21 at 08:22
  • Thank you very very much! I confess that I got a little confused starting at: "One then would assign it as...". Because the `let { properties: { NAMEK: name } } = s;` is doing pretty much what I want to in the sense that it creates a new variable `name`, that holds the value of `s.properties.NAMEK`. :) – Lenn Oct 20 '21 at 09:48