【问题标题】:Destructuring nested geojson object into a list将嵌套的 geojson 对象解构为列表
【发布时间】:2021-12-01 16:36:34
【问题描述】:

我知道有很多类似的问题,但是到目前为止我还没有找到任何解决方案。 所以我有这个对象

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

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

或者有什么更好的方法吗?

【问题讨论】:

  • 您能否详细说明一下,您具体需要什么结构? “NAMEK”代表什么?
  • 如果一个对象的结构对任务很重要,那么将它们全部混合在一行中是一个糟糕的主意。对输入使用正确的格式。如果这会占据一整页,那么这不是一个最小的例子。并且需要去除不必要的属性。
  • 你不能直接将一个对象“解构”成一个数组:Destructure object into an array, Destructure object properties into array values, ...
  • 您能否提供您的尝试并特别强调您遇到的问题?
  • @Lenn .. 从提供的所有解决方案中,还有什么问题吗?

标签: javascript arrays mapping geojson destructuring


【解决方案1】:

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

【讨论】:

  • 谢谢@NickParsons,你说得对!
  • 非常感谢!这也帮助我更好地理解事物!
【解决方案2】:

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

【讨论】:

  • 非常感谢!!再次为迟到的答案感到抱歉,但这有助于我理清思路
  • 只有一个问题:如果我要将 features 中第一个元素的 NAMEK 属性的值提取到新命名的变量 name 中,我尝试了这个 ``` let s = input .特征[1]; console.log(s) let {properties: { NAMEK }: name} = s; ```但这确实是错误的。抱歉,这种破坏真的让我很困惑......
  • 对于所描述的情况,可以像 let { properties: { NAMEK: name } } = s; 一样解构对象,这等效于 let name = s.properties.NAMEK,并表明只初始化 name。然后将其作为条目分配给另一个对象,例如{ name }。但也可以保持前者的解构。然后写{ name: NAMEK }
  • 非常感谢!我承认我从以下开始有点困惑:“然后将其分配为......”。因为let { properties: { NAMEK: name } } = s; 正在做我想做的事情,因为它创建了一个新变量name,它保存了s.properties.NAMEK 的值。 :)
猜你喜欢
  • 1970-01-01
  • 2020-07-14
  • 2017-11-14
  • 2021-11-16
  • 2022-01-08
  • 2023-02-03
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
相关资源
最近更新 更多