繁体   English   中英

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

[英]Filter a part of txt string that not exist in javascript

我在 geojson 多边形文件中有属性,这些属性是工作日的不同组合,例如:Mon-Fri 或 Tue-Fri 或 Tue,Wed,Fri 或 Mon,Thu 或 Wed-Fri 等。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM