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

202
Vistas
Is there a way to set the value of one key as the same as the value of another key in the same object in JS?

I want to know if there is a way to access the value of a key to add as the value of another key in the same object.

For example I want:

obj = {key1: 'apple', key2: 'applesauce'};

I tried obj = {key1: 'apple', key2: this.key1 + 'sauce'}

Also tried obj = {key1: 'apple', key2: key1 + 'sauce'}

I want it so that updating key1 automatically updates key2.

Thanks

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

0

You can do it with a getter.

obj = {
  key1: 'apple',
  get key2() {
    return this.key1 + 'sauce';
  }
}

console.log(obj.key2);
obj.key1 = 'awesome';
console.log(obj.key2);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Basically, not at declaration, not that I know of. You can't refer to the object inside what is basically an object literal using this until the thing actually exists.

You could create a function to generate that object:

function keymaker(key) {
  return {
    key1: key,
    key2: key + 'sauce'
  }
}

obj = keymaker('apple');
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