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

250
Vistas
Javascript: Using the variable's content as a name

I am trying to use the content of a variable as the name of an object property that another function has to save but I can't figure out how to implement it.

  function saveData(val) {
    console.log(val); //Here I need to receive the Object in the format of {key Name: value}, but I am getting the name key, as it is not being interpreted.
  }
  const arr = [
    { name: "John", lastname: "Doe" },
    { name: "John2", lastname: "Doe2" }
  ];
  for (const [key, value] of Object.entries(arr)) {
    saveData({ key: value });
  }

I cannot modify saveData, so how can I send {key: value} in a way that key is interpreted and its content is sent?

Here you can find the CodeSandBox example

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

0

You can use ES6 Computed Property Names:

for (const [key, value] of Object.entries(arr)) {
    // You can use any expression inside []
    saveData({ [key]: value });
  }
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use initializer notation for this. It allows you to create "computed properties".

The syntax is {[variable]: value}, so your saveData call would become saveData({ [key]: 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