【问题标题】:Filter geojson TS过滤geojson TS
【发布时间】:2019-12-04 01:55:05
【问题描述】:

我使用 angular 8。我的资产文件夹中有一个 geojson 填充:

{ "type": "FeatureCollection",
  "features": [
    { "type": "Feature", "geometry": {"type": "Point", "coordinates": [9.15926605,41.38718765]},"properties": {"f":"2A0000212","n":"HOPITAL LOCAL DE BONIFACIO","c":"20169 BONIFACIO","t":"2"}},
    { "type": "Feature", "geometry": {"type": "Point", "coordinates": [9.15926605,41.38718765]},"properties": {"f":"2A0003141","n":"ANTENNE DU SMUR  BONIFACIO","c":"20169 BONIFACIO","t":"2"}},
    ...
]}

我想通过属性“t”过滤我的 geojson 数据(使用 mat-select-form 和 NgModel。

例如,过滤 json 中具有属性 "t" = 2 的项目

//Component.ts

json;

constructor( private http: HttpClient) {}

ngOnInit() {this.http.get('assets/es.json').subscribe((json: any) => { this.json = json;});}

【问题讨论】:

  • this post 的答案应该可以帮助您朝着正确的方向前进。
  • 谢谢我试着理解答案^^

标签: angular typescript filter rxjs geojson


【解决方案1】:

以下 rxjs 操作符应该按照你想要的方式过滤:

import { of } from 'rxjs'; 
import { filter, flatMap, map } from 'rxjs/operators';

let filteredData = this.http.get('assets/es.json').pipe(
  map(response => JSON.parse(response)),
  flatMap(obj => obj.features),
  filter((feature: any) => feature.properties.t == 2)
);

filteredData.subscribe(x => {
  console.log(x);
});

【讨论】:

  • 谢谢..我用过滤器测试我有这个错误:core.js:6014 ERROR TypeError: Cannot read property 't' of undefined
  • 每个项目的“属性”中是否总是有一个“t”?每个项目都有“属性”吗?如果没有,您可以将该行更改为 filter((feature: any) => feature.properties && feature.properties.t == 2)
  • 是的,我一直都有属性和 t
  • 这里是堆栈闪电战:stack59162128.stackblitz.io 如果您收到“无法读取未定义的属性 't'”,那么您的一个或多个数据记录没有“属性”属性或结构为稍微不一样。使用过滤器,使其与数据集中出现的不同案例相匹配。
猜你喜欢
  • 2017-09-20
  • 2014-12-31
  • 1970-01-01
  • 2013-04-15
  • 2017-09-25
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多