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

274
Vistas
Define object field name based on variable value in javascript

Is possible in javascript define an object field name based on the value of a variable INLINE?

for exemple:

const field = "name";

const js = {field:"rafael"};

console.log(js);

the result of this code will be {field:rafael} but the result I want is {name:rafael}.

I know I can do

const field = "name";

const js = {};
js[field] = "rafael";

but i would like to do inline as I initialize the object. Is that possible?

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

0

The es6 version of JavaScript allows you to handle this issue, we can use variables while creating the object to dynamically set the property like so:

const field = "name";

const js = {[field] : "rafael"};

console.log(js);

Setting dynamic property keys - https://www.c-sharpcorner.com/blogs/how-to-set-dynamic-javascript-object-property-keys-with-es6

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