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

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

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 Relatório

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