按 属性 过滤 GeoJson 数据以获得数据子集

Filtering GeoJson data by property to get a subset of data

我正在尝试通过 属性 获取 Geojson 数据的子集。

 {
   "type": "FeatureCollection",
   "features": [
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -115.5578333,32.9646667 ]
    },
    "properties": {
    "Year1979_03":2.92606854,
    "Year1979_06":2.963032273,
    "Year1979_09":2.968127935
    }
  }]

如果用户选择年份 "Year1979_03 那么它应该 return

{
       "type": "FeatureCollection",
       "features": [
      {
        "type": "Feature",
        "geometry": {
           "type": "Point",
           "coordinates":  [ -115.5578333,32.9646667 ]
        },
        "properties": {
        "Year1979_03":2.92606854,
        }
      }]

不确定我是否正确理解了你的问题,但如果你有用户输入并且将其保存在变量中:

let userInput = "Year1979_03"

并且您有原始形式的返回数据

let originalData = {
   "type": "FeatureCollection",
   "features": [
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -115.5578333,32.9646667 ]
    },
    "properties": {
        "Year1979_03":2.92606854,
        "Year1979_06":2.963032273,
        "Year1979_09":2.968127935
    }
   }
  ]
 }

然后你可以使用es6展开运算符创建你想创建的对象:

let reformedData = {
    ...originalData, 
    "features": [
    {
     ...originalData.features[0], 
     "properties": 
    {
     [userInput]: originalData.features[0].properties[userInput] 
    } 
   }] 
  }

上面,我使用扩展运算符从 originalData 对象中检索我想要保留的所有键和值,但指定了我想要 change/replace 的键和值。这会给你带来你想要的对象。