Leaflet filtering breaks with indexOf() or search()

I have a map with a lot of Markers imported from GeoJSON via AJAX and I can’t get filtering to work.

There are 3 checkboxes and any combination of them can be checked. Each Marker has a property called “types” that may contain any combination of the 3 values (it is a map of shops that may have soft-serve, scoops and/or milkshake so ” soft scoop shake”, ” soft scoop”, ” soft shake”, ” scoop shake”, ” scoop”, ” shake”).

  1. So first I check which values are checked and add them into a string.
  2. Then I check if none are checked and if so all Markers should be shown.
  3. Then I check if the Marker has all 3 values in which case it should always be shown (as it fulfills all values).
  4. Finally I check if the specific selected values are on the Marker. It works fine if it is precisely the same, but if option 3 is the only selected value and a Marker has both options 2 and 3 it should still be shown.

First line with == works fine, but the moment I try something in the style of indexOf() or search(), everything breaks and nothing loads at all.

I have tried working with arrays as well, but the filter section of Leaflet is not exactly well described and JavaScript is not my “first language”.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var geojsonLayer = new L.GeoJSON.AJAX("data/map.geojson", {
pointToLayer: pointToLayer,
filter: icefilter
});
function icefilter(json) {
var icetypeFilter = "";
var cond1 = true;
$("input[name=icetype]").each(function () {
if (this.checked) {
icetypeFilter = icetypeFilter + " " + this.value;
}
});
var att = json.properties.types;
if (icetypeFilter.length == 0) {
cond1 = true;
} else if (att == " soft scoop shake") {
cond1 = true;
} else {
cond1 = att == icetypeFilter; //Works fine with this one
//cond1 = att.indexOf(icetypeFilter) >= 0; //The moment I activate this line and remove the one above everything is broken and no markers are shown - even with no filters selected.
}
return cond1;
}
$("input[name=icetype]").click(function () {
clusterLayer.clearLayers();
geojsonLayer.refresh();
locationList();
});
</code>
<code>var geojsonLayer = new L.GeoJSON.AJAX("data/map.geojson", { pointToLayer: pointToLayer, filter: icefilter }); function icefilter(json) { var icetypeFilter = ""; var cond1 = true; $("input[name=icetype]").each(function () { if (this.checked) { icetypeFilter = icetypeFilter + " " + this.value; } }); var att = json.properties.types; if (icetypeFilter.length == 0) { cond1 = true; } else if (att == " soft scoop shake") { cond1 = true; } else { cond1 = att == icetypeFilter; //Works fine with this one //cond1 = att.indexOf(icetypeFilter) >= 0; //The moment I activate this line and remove the one above everything is broken and no markers are shown - even with no filters selected. } return cond1; } $("input[name=icetype]").click(function () { clusterLayer.clearLayers(); geojsonLayer.refresh(); locationList(); }); </code>
var geojsonLayer = new L.GeoJSON.AJAX("data/map.geojson", {
    pointToLayer: pointToLayer,
    filter: icefilter
});     

function icefilter(json) {
  var icetypeFilter = "";
  var cond1 = true;

  $("input[name=icetype]").each(function () {
    if (this.checked) {
      icetypeFilter = icetypeFilter + " " + this.value;
    }
  });
  var att = json.properties.types;
  if (icetypeFilter.length == 0) {
    cond1 = true;
  } else if (att == " soft scoop shake") {
    cond1 = true;
  } else {
    cond1 = att == icetypeFilter; //Works fine with this one
    //cond1 = att.indexOf(icetypeFilter) >= 0; //The moment I activate this line and remove the one above everything is broken and no markers are shown - even with no filters selected.  
  }
  return cond1;
}

$("input[name=icetype]").click(function () {
  clusterLayer.clearLayers();
  geojsonLayer.refresh();
  locationList();
});

8

IIUC, you want to implement an AND filter with your 3 checkboxes. Your GeoJSON data contains Features with properties.types field as string of the form " soft scoop shake" (may contain fewer words).

cond1 = att.indexOf(icetypeFilter) >= 0;

The moment I activate this line […] everything is broken and no markers are shown – even with no filters selected.

What probably happens is that some of your Features lack the properties.types field. So att is undefined, and att.indexOf() raises an error and stops your script (same with any other method call, like att.search().

An “immediate” solution could consist in making sure you always have att variable as string:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var att = json.properties.types;
if (typeof att !== "string") {
att = ""; // Ensure we always have a string
}
</code>
<code>var att = json.properties.types; if (typeof att !== "string") { att = ""; // Ensure we always have a string } </code>
var att = json.properties.types;
if (typeof att !== "string") {
  att = ""; // Ensure we always have a string
}

That being said, you will have troubles if properties.types does not always lists words in the same order, or if you do not loop over your checkboxes in that exact same order as well.

E.g. a Feature could have " shake scoop" types, but the checkboxes request " scoop shake".

You should not concatenate your checkboxes values in a string, as it forces some order which may not exactly match your Features, even if they have the correct words (but in a different order).

Filling an array, you could do something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var icetypeFilter = []; // Use an array
$("input[name=icetype]").each(function () {
if (this.checked) {
icetypeFilter.push(icetypeFilter + " " + this.value);
}
});
// ...
return icetypeFilter.every(checkValue => att?.contains(checkValue));
</code>
<code>var icetypeFilter = []; // Use an array $("input[name=icetype]").each(function () { if (this.checked) { icetypeFilter.push(icetypeFilter + " " + this.value); } }); // ... return icetypeFilter.every(checkValue => att?.contains(checkValue)); </code>
var icetypeFilter = []; // Use an array

$("input[name=icetype]").each(function () {
  if (this.checked) {
    icetypeFilter.push(icetypeFilter + " " + this.value);
  }
});

// ...
return icetypeFilter.every(checkValue => att?.contains(checkValue));

Here using .every() method:

every acts like the “for all” quantifier in mathematics. In particular, for an empty array, it returns true.

So it could replace your 3 checks.


Then you can later improve by using Regular Expressions, etc.

1

Just if anyone wanted to see the code I ended up with…

It does not solve the problem with locations without the “types” value that was the primary cause but that was solved elsewhere where the geojson is generated.

Had I not done that already @ghybs solution with checking if it is there would have been even better.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function icefilter (json) {
var att = json.properties.types;
var cond = true;
$("input[name=icetype]").each(function(){
if (this.checked) {
cond = cond * att.includes(this.value);
}
});
return(cond);
}
</code>
<code>function icefilter (json) { var att = json.properties.types; var cond = true; $("input[name=icetype]").each(function(){ if (this.checked) { cond = cond * att.includes(this.value); } }); return(cond); } </code>
function icefilter (json) {
    var att = json.properties.types;
    var cond = true;
    $("input[name=icetype]").each(function(){
        if (this.checked) {
            cond = cond * att.includes(this.value);
        }
    });
    return(cond);
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật