Atlaslog
Atlaslog / Field Notes / Styling & rendering
Field note · Styling & rendering

A numeric filter never matches because the property is a string

Quick answer

A filter for districts with `pop >= 10000` draws nothing even though 40 features qualify, and no error is logged. The `pop` values arrived from CSV as strings, and Mapbox expressions are strongly typed with no implicit conversion.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: Mapbox GL JS 2.15 / 3.x / MapLibre 4.x
Symptom
Checklist
  1. Print `typeof` of the property on a feature from `querySourceFeatures`
  2. Remove the filter to confirm it is filtering rather than styling
  3. Wrap the value in `['to-number', ['get', 'pop']]` and retest
  4. Check how the source was produced, since CSV-to-GeoJSON usually keeps strings
  5. Compare the filter literal type with the actual property type
Fix

Convert explicitly in the expression: `filter: ['>=', ['to-number', ['get', 'pop'], 0], 10000]`, and quote string comparisons (`['==', ['get', 'level'], '2']`); also coerce numeric fields to `Number` at ingest.

Code
filter: ['>=', ['get', 'pop'], 10000]
const f = map.querySourceFeatures('districts')[0];
console.log(f.properties.pop, typeof f.properties.pop);
// "12000" string
map.setFilter('districts', null);   // 要素出现 -> 过滤条件有问题
Watch out
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

Atlaslog (2026). A numeric filter never matches because the property is a string. https://www.jingweizhi.com/en/cookbook/filter-number-vs-string.html (accessed 2026-11-03).