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

88
Visualizações
Call function from as a string in nodejs

I have this array of object:

let choices = [
    {title: 'Single Card Payment', func: 'singleCard'},
    {title: 'Monthly Card Payment', func: 'monthlyCard' },
    {title: 'Monthly Direct Debit Payment', func: 'monthlyDD'}
]

I'm looping through with it and I would like to call functions with the value of the func, like this (which is obviously wrong)

for(choice for choices) {
    choice.func('some', 'params');
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can create an object with methods. Like below

const funcs = {
    singleCard: () => { console.log('single card called ')}
}

And then call like this

for (i in choices) {
    funcs[choices[i].func]()
}
about 4 years ago · Juan Pablo Isaza Relatório

0

using evil function:

    let choices = [
    {title: 'Single Card Payment', func: 'singleCard'},
    {title: 'Monthly Card Payment', func: 'monthlyCard' },
    {title: 'Monthly Direct Debit Payment', func: 'monthlyDD'}
]
for(i in choices) {
    eval(choices[i].func)('some', 'params');
}

edit: it's eval guys not evil. 0K.

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