【问题标题】:Filter a part of txt string that not exist in javascript过滤javascript中不存在的部分txt字符串
【发布时间】:2018-10-05 12:04:03
【问题描述】:

我在 geojson 多边形文件中有一些属性,这些属性是工作日的不同组合,如下所示: Mon-Fri or Tue-Fri or Tue,Wed,Fri or Mon,Thu or Wed-Fri etc..

我需要在 Leaflet 中显示文本字符串中没有值“Mon”(星期一)的多边形。 我怎样才能过滤掉这个?我正在将此代码用于其他过滤...

var non_mon = new L.layerGroup();
$.getJSON("..data/polygons.geojson", function(json) {
 var vectorGrid =  L.vectorGrid.slicer(json, {
                  maxZoom: 20,
                  rendererFactory: L.svg.tile,
                  vectorTileLayerStyles: {
                      sliced: function(properties, zoom){
                      var dayint = properties.Days_1

                      if (dayint = "does not have Mån" ){
                        return{
                      weight: 0.5,
                      color: '#ffffff',
                      opacity: 1,
                      fill: true,
                      fillColor: '#ff0000',
                      stroke: true,
                      fillOpacity: 0.6
                      }
                      } else {
                      return {
                      weight: 0,
                      fill: false,
                      stroke: false    
                      }
                     }
                     }},
                     interactive: true,
                   })
     .on('click', function(e) {
  var properties = e.layer.properties;
  L.popup()
    .setContent(
      "<b>Weekdays</b>" + '\xa0\xa0' + properties.Days_1 + '</b>' +
      "<br>Date from: " + '<i>' + properties.Date + '</i>' )
    .setLatLng(e.latlng)
    .openOn(map);
                   })
vectorGrid.addTo(non_mon)

            }) 

这就是 GeoJSON 的样子

{ "type": "Feature", "properties": { "Days_1": "Mån-Fre" },
{ "type": "Feature", "properties": { "Days_1": "Tis-Fre" },
{ "type": "Feature", "properties": { "Days_1": "Ons-Fre" },
{ "type": "Feature", "properties": { "Days_1": "Tors,Fre" },
{ "type": "Feature", "properties": { "Days_1": "Mån,Ons,Fre" },
{ "type": "Feature", "properties": { "Days_1": "Tis,Ons-Fre" },
{ "type": "Feature", "properties": { "Days_1": "Ons,Tors" },
{ "type": "Feature", "properties": { "Days_1": "Mån" },
{ "type": "Feature", "properties": { "Days_1": "Tis" },
{ "type": "Feature", "properties": { "Days_1": "Ons" },
{ "type": "Feature", "properties": { "Days_1": "Tors" },
{ "type": "Feature", "properties": { "Days_1": "Fre" },
{ "type": "Feature", "properties": { "Days_1": "Tis-Tors" },
{ "type": "Feature", "properties": { "Days_1": "Mån-Tors" },
{ "type": "Feature", "properties": { "Days_1": "Ons,Fre" },
{ "type": "Feature", "properties": { "Days_1": null },

【问题讨论】:

  • 您能否发布一些示例 GeoJSON,以便明确格式?
  • @Mathyn,完成,确实将“Weekdays_1”更改为更正确的“Days_1”属性
  • 我猜是更好的样本.... 剥离但更多的是工作日的格式。需要找到那些没有“Mån”的行,这意味着驱动程序列表中没有星期一

标签: javascript string leaflet geojson


【解决方案1】:

好的,因此您要过滤“Days_1”属性不包含字符串“Mån”的所有行。你可以这样做:

var geoJson = [...]; // Your GeoJSON

// After this function call 'filtered' contains the filtered list of GeoJSON features
var filtered = geoJson.filter(
    function(element) {
        // We are only interested in elements which do not contain 'Mån'
        return element.indexOf("Mån") != -1;
    }
);

在本例中,我们使用数组原型的“过滤器”方法部分。 filter 方法将为数组中的每个元素调用传递的函数。然后,您可以返回 true 以保留元素或返回 false 以过滤元素。最终结果将作为新数组返回。

【讨论】:

  • 无法实施您的建议,我收到 geoJson 无效的错误消息。如果不匹配,vectorGrid 使用 function(json)。它是一个大多边形(27Mb)GeoJson 文件,所以我需要使用 vectorGrid 函数来不减慢网页速度。我添加了一行“getJSON”,它应该从一开始就存在。任何将您的建议实施到这一行的建议......顺便说一下,如果这会产生问题,属性中还有“null”值......
  • 我对 Leaflet 不是很熟悉,但该错误似乎表明您传递给 VectorGrid 构造函数的 GeoJSON 存在某种错误(例如,您已经说过无效的 GeoJSON)。请仔细检查documentation,看看您是否犯了错误。我猜在这种情况下,您需要将 GeoJSON 作为“FeatureCollection”传递。
猜你喜欢
  • 1970-01-01
  • 2019-09-19
  • 1970-01-01
  • 2017-02-27
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 2017-02-17
相关资源
最近更新 更多