首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >过滤javascript中不存在的txt字符串的一部分

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

Stack Overflow用户
提问于 2018-10-03 16:58:53
回答 1查看 133关注 0票数 1

我在一个geojson多边形文件中有不同组合的属性,比如:星期一-星期五或者星期二-星期五或者星期二,星期三,星期五或者星期一,清华或者星期三等等。

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

代码语言:javascript
复制
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的外观

代码语言:javascript
复制
{ "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 },
EN

回答 1

Stack Overflow用户

发布于 2018-10-04 03:05:09

好了,你想要过滤所有'Days_1‘属性不包含字符串’M­n‘的行。你可以这样做:

代码语言:javascript
复制
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”方法部分。filter方法将为数组中的每个元素调用传递的函数。然后,可以返回true以保留该元素,或返回false以过滤该元素。最终结果将以新数组的形式返回。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52623315

复制
相关文章

相似问题

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