Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
How can I reference a JavaScript variable as a value in an expression with Mapbox?

new to mapbox and enjoying my journey so far.

I am curious if there is a way to reference a javascript variable within an expression.

I am trying to essentially reference some color values that are dynamic, However I've had no luck. I went through the MB docs but I havent been able to find information or options that match my conundrum. Thank you for any info, pointers.

const mbFillColorExpressionValue = Object.keys(fills)
   .map(fips => {
      return `"${fips}","${fills[fips].fill}"`;
   })
   .concat('"white"')
   .toString();

(mbFillColorExpressionValue returns a string "18089","#839EBE","18091","#839EBE","18127","#839EBE","white")


  this.map.setPaintProperty('counties-towns', 'fill-color', [
     'match',
     ['get', 'GEOID'],
     geoids,
     mbFillColorExpressionValue
   ]);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After reading the docs of Mapbox, I think the end result should look something like the code below. Each GEOID is paired with its corresponding fill. And the color white as a fallback on the last line.

this.map.setPaintProperty('counties-towns', 'fill-color', [
  'match',
   ['get', 'GEOID'],
   '18089', '#839EBE',
   '18091', '#839EBE',
   '18127', '#839EBE',
   'white'
]);

To get to that result we first have to change the map method to a flatMap. This is because we can't add any keys when mapping over an array, but we can return an array for each item and the flatten it to turn the whole thing into a single array.

const mbFillColorExpressionValue = Object.keys(fills).flatMap(fips => [fips, fills[fips].fill]);
mbFillColorExpressionValue.push('white');

From here we can use the spread syntax ... to place the contents of the mbFillColorExpressionValue array into the expression.

this.map.setPaintProperty('counties-towns', 'fill-color', [
  'match',
   ['get', 'GEOID'],
   ...mbFillColorExpressionValue
]);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!