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
Call Javascript functions from variable string name

I am looking for the best method of calling functions with variable names that all follow a specific rule. I am trying to avoid using eval() since there are so many problems about it online.

I have a system setup where different groups have a function that can be called based on their name EG Test group would have a function imported in called getValue_Test(params) and this function can be called for any group name EG getValue_Test2(params) would work if the group name was test2.

I have these functions being imported into the react component and just want to know the best way to call them without making giant switch statement since there will be hundreds of these functions.

Any help would be appreciated :) Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you've got some functions defined like this:

function foo() {}

function bar() {}

function baz() {}

You could put them in an object (or map):

const funcs = { foo, bar, baz };

Then you will be able to use a string to access and eventually call the function:

funcs["foo"](); // called foo

const key = "bar";

funcs[key](); // called bar

funcs.baz(); // called baz

However this is usually not recommended. It looks like you might want to use an array instead? Your function names differ only by a number that appears to be incrementing.

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!