Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!