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

195
Vistas
How can I create a dynamic object in a literal?

I have an object which includes several fields, most values are strings (constants) but I also want to use a variable (populated using some state)

const {order} = this.state;

myObject={{
    fieldA: 2,
    fieldB: ${order.value}
}}

I tried different variation of the above (with/without single quotes, etc.) but I am never able to set the value I need.

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

0

You are adding 2 brackets for an object which is not valid and no need of template literal.

const { order } = this.state;

const myObject = {
  fieldA: 2,
  fieldB: order.value,
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

If this.state looks like this:

this.state = {order: ""}

Declaring

const {order} = this.state

would just create a constant called order that is holding the value of this.state.order at the time order was declared. It will not be updated when this.state.order is updated.

If you want to set myObject.fieldB to hold a reference to the value of this.state.order, that is, the state value of order, do this:

const myObject = {
  fieldA: 2,
  fieldB: this.state.order,
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You have used two braces ('{}') which are not needed. The code should be like below

const myobject = {fieldA: 2, fieldB: order.value};
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