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

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

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 Denunciar

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