javascript - 过滤javascript中不存在的txt字符串的一部分

标签 javascript string leaflet geojson

我在一个 geojson 多边形文件中有属性,这些属性是工作日的不同组合,如下所示: 周一至周五或周二至周五或周二、周三、周五或周一、周四或周三至周五等。

我需要的是在 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 },

最佳答案

好的,所以您要筛选“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;
    }
);

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

关于javascript - 过滤javascript中不存在的txt字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52623315/

相关文章:

javascript - Spread 运算符动态属性更新

javascript - 无服务器:通过调用方法触发并忘记不能按预期工作

javascript - 在 AngularJS 应用程序中使用 $http 从 post 请求中去除 header 字段

c - C中的递归strstr函数

html - 在Swift 2.0中读取HTML文件

javascript - Leaflet - 在 map 的某些区域单击即可执行某些操作

javascript - 重置表单、重置字段值、重置全部焦点 onSubmit()

ios - 我的 AVPlayer 的本地路径视频出现字符串错误

coffeescript - 有没有办法确保在执行 fitbounds 后打开的弹出窗口位于 View 内

javascript - 从特定飞行计划航路点获取 GPS 坐标