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

124
Vistas
is there a way to use strings with some object's property in javascript?

it's hard to explain, bur lets go on with a example

const exampleProperty = {
    log1: () => {
        console.log('log1')
    },
    log2: () => {
        console.log('log2')
    },
    log3: () => {
        console.log('log3')
    },
    log4: () => {
        console.log('log4')
    }
}
for (let i = 0; i < 4; i++) {
    // now here I want to run all the functions in the example Property
    // I imagined it something like
    exampleProperty.log${i}
    // but this is invalid syntax
}

There might be a real easy answer to this but as I am new i really can't imagine what to do

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you wanted to enumerate every method

const exampleProperty = {
    log1: () => {
        console.log('log1')
    },
    log2: () => {
        console.log('log2')
    },
    log3: () => {
        console.log('log3')
    },
    log4: () => {
        console.log('log4')
    }
}

Object.values(exampleProperty).forEach(p => p())

about 4 years ago · Juan Pablo Isaza Denunciar

0

Working example, using backticks

const exampleProperty = {
    log1: () => {
        console.log('log1')
    },
    log2: () => {
        console.log('log2')
    },
    log3: () => {
        console.log('log3')
    },
    log4: () => {
        console.log('log4')
    }
}
for (let i = 1; i <= 4; i++) {
    // now here I want to run all the functions in the example Property
    // I imagined it something like
    exampleProperty[`log${i}`]();
    // but this is invalid syntax
}

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