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

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

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 Denunciar

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 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