Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda