Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda