首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript -根据其他json的值过滤geojson

Javascript -根据其他json的值过滤geojson
EN

Stack Overflow用户
提问于 2019-06-02 21:03:49
回答 1查看 992关注 0票数 1

我正在努力根据另一个JSON的值过滤一个geojson。具体地说,我正在尝试根据另一个json中的id过滤geojson。每次,我过滤后的geojson都会返回null。

我已经创建了一个JSfiddle here来显示我想要做的事情。

代码是

代码语言:javascript
复制
    mygeojson={crs: {properties: {name: "EPSG:4326"}, type: "name"},
features: [
    {geometry: {coordinates: [25, 37], type: "Point"},properties: {"wineryid":3},type:"Feature"},
  {geometry: {coordinates: [26, 37], type: "Point"},properties: {"wineryid":45},type:"Feature"},
  {geometry: {coordinates: [25, 38], type: "Point"},properties: {"wineryid":34},type:"Feature"},
  {geometry: {coordinates: [28, 37], type: "Point"},properties: {"wineryid":42},type:"Feature"},
  {geometry: {coordinates: [25, 342], type: "Point"},properties: {"wineryid":80},type:"Feature"},
  {geometry: {coordinates: [25.24, 37.12], type: "Point"},properties: {"wineryid":120},type:"Feature"}], type:"FeatureCollection"};
mylist=[{id:1, name:"test1"},{id:34,name:"test2"}];
tempgeojson = mygeojson.features.filter(function (item) {
  return (mylist.every(f => item.properties.id === f.id) )
});
filteredgeojson={};
filteredgeojson.features=tempgeojson;
filteredgeojson.crs=mygeojson.crs;
filteredgeojson.type="FeatureCollection";

我怎样才能得到我需要的过滤过的geojson?(在本例中,预期的geojson大约是id的1和34。)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-02 21:48:38

尝试使用以下代码进行要素过滤

代码语言:javascript
复制
filteredgeojson.features = mygeojson.features.filter(item => { 
     if(mylist.filter(myitem => myitem.id === item.properties.wineryid).length > 0) { 
          return item;
      }
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56415323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档