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

170
Views
How can i make this condition dynamic?

fieldName is a variable and is dynamic and the value can be either media, media2 or media3.

How can i make "media" be either media, media1 or media3.

instead of doing this...

if(fieldName === 'media') {
const newObj = { ...objComponent, media: json.media };
} if(fieldName === 'media2') {
const newObj = { ...objComponent, media2: json.media };
} else {
const newObj = { ...objComponent, media3: json.media };
}
almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can pass key with brackets [] like below

const newObj = { ...objComponent, [fieldName]: json.media };

That would help you pass dynamic string keys to your object.

almost 4 years ago · Santiago Trujillo Report

0

You can use object's dynamic keys, where variable name can be used as key inside the object. The syntax is surrounding the key with [] and the variable as the key.

// fieldName = media or media2 or media3
const newObj = { ...objComponent, [fieldName]: json.media};
almost 4 years ago · Santiago Trujillo 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!